private int InternalRead(int length, out sbyte[] result) { IntPtr resultPtr = AndroidJNI.NewSByteArray(length); int offset = 0; int bytesLeft = length; while (bytesLeft > 0) { s_InternalReadArgs[0] = new jvalue() { l = resultPtr }; s_InternalReadArgs[1] = new jvalue() { i = offset }; s_InternalReadArgs[2] = new jvalue() { i = bytesLeft }; int bytesRead = AndroidJNI.CallIntMethod(m_FileStreamRawObject, s_InternalReadMethodId, s_InternalReadArgs); if (bytesRead <= 0) { break; } offset += bytesRead; bytesLeft -= bytesRead; } result = AndroidJNI.FromSByteArray(resultPtr); AndroidJNI.DeleteLocalRef(resultPtr); return(offset); }
public override int Read(byte[] buffer, int offset, int count) { lock (_lock) { int ret = 0; IntPtr array = IntPtr.Zero; try { #if UNITY_2019_3_OR_NEWER array = AndroidJNI.NewSByteArray(count); #else array = AndroidJNI.NewByteArray(count); #endif var method = AndroidJNIHelper.GetMethodID(inputStream.GetRawClass(), "read", "([B)I"); ret = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), method, new[] { new jvalue() { l = array } }); #if UNITY_2019_3_OR_NEWER sbyte[] data = AndroidJNI.FromSByteArray(array); #else byte[] data = AndroidJNI.FromByteArray(array); #endif Array.Copy(data, 0, buffer, offset, ret); } finally { if (array != IntPtr.Zero) { AndroidJNI.DeleteLocalRef(array); } } return(ret); } }
/// <summary> /// Call from java get asset file bytes and convert to string /// </summary> /// <param name="path"></param> /// <returns></returns> public static string GetAssetString(string path) { #if !KENGINE_DLL && UNITY_ANDROID #if UNITY_2017_1_OR_NEWER var clsPtr = AndroidJNI.FindClass("com/github/KEngine/AndroidHelper"); var methondPtr_getAssetBytes = AndroidJNIHelper.GetMethodID(clsPtr, "getAssetBytes", "(Ljava/lang/String;)[B", true); var arg1 = new object[] { path }; var args = AndroidJNIHelper.CreateJNIArgArray(arg1); var byteArray = AndroidJNI.CallStaticObjectMethod(clsPtr, methondPtr_getAssetBytes, args); var bytes = AndroidJNI.FromSByteArray(byteArray); AndroidJNIHelper.DeleteJNIArgArray(arg1, args); AndroidJNI.DeleteLocalRef(byteArray); AndroidJNI.DeleteLocalRef(clsPtr); return(bytes.ToString()); #else return(AndroidHelper.CallStatic <string>("getAssetString", path)); #endif #else ErrorNotSupport(); return(null); #endif }
private void UpdateBleDevice(IntPtr device) { if (device == IntPtr.Zero) { return; } AndroidJNI.PushLocalFrame(64); var blitMethod = AndroidJNI.GetMethodID(bleDeviceCls, "blit", "()V"); var getAddrMethod = AndroidJNI.GetMethodID(bleDeviceCls, "getAddress", "()Ljava/lang/String;"); var readNumMethod = AndroidJNI.GetMethodID(bleDeviceCls, "getReadNum", "()I"); var getCharacteristicMethod = AndroidJNI.GetMethodID(bleDeviceCls, "getCharacteristicFromReadData", "(I)Ljava/lang/String;"); var getServiceUuidMethod = AndroidJNI.GetMethodID(bleDeviceCls, "getServiceUuidFromReadData", "(I)Ljava/lang/String;"); var isNotifyMethod = AndroidJNI.GetMethodID(bleDeviceCls, "isNotifyReadData", "(I)Z"); var getReadDataMethod = AndroidJNI.GetMethodID(bleDeviceCls, "getDataFromReadData", "(I)[B"); string addr = AndroidJNI.CallStringMethod(device, getAddrMethod, null); this.UpdateCharastricsKeys(addr, device); // read Charastrics Data AndroidJNI.CallVoidMethod(device, blitMethod, null); int readNum = AndroidJNI.CallIntMethod(device, readNumMethod, null); for (int i = 0; i < readNum; ++i) { this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(i)); string serviceUuid = AndroidJNI.CallStringMethod(device, getServiceUuidMethod, argBuilder.Build()); string charastristic = AndroidJNI.CallStringMethod(device, getCharacteristicMethod, argBuilder.Build()); bool isNotify = AndroidJNI.CallBooleanMethod(device, isNotifyMethod, argBuilder.Build()); var dataObj = AndroidJNI.CallObjectMethod(device, getReadDataMethod, argBuilder.Build()); var sbytes = AndroidJNI.FromSByteArray(dataObj); var characteristicData = new BleCharacteristicData(addr, serviceUuid, charastristic, sbytes, isNotify); this.readDatas.Add(characteristicData); } AndroidJNI.PopLocalFrame(IntPtr.Zero); }
public override int Read(byte[] buffer, int offset, int count) { lock (_lock) { int ret = 0; IntPtr array = IntPtr.Zero; try { array = AndroidJNI.NewSByteArray(count); var method = AndroidJNIHelper.GetMethodID(inputStream.GetRawClass(), "read", "([B)I"); ret = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), method, new[] { new jvalue() { l = array } }); var data = AndroidJNI.FromSByteArray(array); if (ret >= 0) { //Array.Copy(data, 0, buffer, offset, ret); Buffer.BlockCopy(data, 0, buffer, offset, ret); position += ret; } else { return(0); } } finally { if (array != IntPtr.Zero) { AndroidJNI.DeleteLocalRef(array); } } return(ret); } }