Ejemplo n.º 1
0
        public void ConnectRequest(string addr)
        {
            var connectMethod = AndroidJNI.GetMethodID(this.bleManagerCls, "connect", "(Ljava/lang/String;)Lcom/toio/ble/BleDeviceObj;");

            this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(addr));
            AndroidJNI.CallObjectMethod(this.javaBleManagerObj, connectMethod, this.argBuilder.Build());
        }
Ejemplo n.º 2
0
        public void UpdateDisconnectedDevices()
        {
            var updateDisconnectedMethod = AndroidJNI.GetMethodID(this.bleManagerCls,
                                                                  "updateDisconnected", "()V");
            var getDisconnectedNumMethod = AndroidJNI.GetMethodID(this.bleManagerCls,
                                                                  "getDisconnectedDeviceNum", "()I");
            var getDisconnectedDeviceAddr = AndroidJNI.GetMethodID(this.bleManagerCls,
                                                                   "getDisconnectedDeviceAddr", "(I)Ljava/lang/String;");

            AndroidJNI.CallVoidMethod(this.javaBleManagerObj, updateDisconnectedMethod, null);
            disconnectedDevices.Clear();

            int num = AndroidJNI.CallIntMethod(this.javaBleManagerObj, getDisconnectedNumMethod, null);

            for (int i = 0; i < num; ++i)
            {
                this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(i));
                string addr = AndroidJNI.CallStringMethod(this.javaBleManagerObj,
                                                          getDisconnectedDeviceAddr, this.argBuilder.Build());
                this.disconnectedDevices.Add(addr);
            }

            foreach (var addr in disconnectedDevices)
            {
                this.charastericsKeyInfos.Remove(addr);
            }
        }
Ejemplo n.º 3
0
        private void UpdateCharastricsKeys(string addr, IntPtr device)
        {
            List <BleCharastericsKeyInfo> list = null;

            if (this.charastericsKeyInfos.TryGetValue(addr, out list))
            {
                return;
            }
            var blitCharaMethod = AndroidJNI.GetMethodID(this.bleDeviceCls, "blitChara", "()V");
            var getKeyNumMethod = AndroidJNI.GetMethodID(this.bleDeviceCls, "getKeysNum", "()I");
            var getServiceUuidFromKeysMethod = AndroidJNI.GetMethodID(this.bleDeviceCls,
                                                                      "getServiceUuidFromKeys", "(I)Ljava/lang/String;");
            var getCharastricUuidFromKeysMethod = AndroidJNI.GetMethodID(this.bleDeviceCls,
                                                                         "getCharastricUuidFromKeys", "(I)Ljava/lang/String;");

            // blit chara
            AndroidJNI.CallVoidMethod(device, blitCharaMethod, null);
            int num = AndroidJNI.CallIntMethod(device, getKeyNumMethod, null);

            if (num <= 0)
            {
                return;
            }
            list = new List <BleCharastericsKeyInfo>(num);
            for (int i = 0; i < num; ++i)
            {
                this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(i));
                string serviceUuid    = AndroidJNI.CallStringMethod(device, getServiceUuidFromKeysMethod, argBuilder.Build());
                string charastricUuid = AndroidJNI.CallStringMethod(device, getCharastricUuidFromKeysMethod, argBuilder.Build());
                var    keyInfo        = new BleCharastericsKeyInfo(addr, serviceUuid, charastricUuid);
                list.Add(keyInfo);
            }

            this.charastericsKeyInfos.Add(addr, list);
        }
Ejemplo n.º 4
0
        public void UpdateScannerResult()
        {
            AndroidJNI.PushLocalFrame(32);
            var scanner                   = GetScanner();
            var blitMethod                = AndroidJNI.GetMethodID(this.bleScannerCls, "blit", "()V");
            var getDeviceNumMethod        = AndroidJNI.GetMethodID(this.bleScannerCls, "getDeviceNum", "()I");
            var getDeviceAddrMethod       = AndroidJNI.GetMethodID(this.bleScannerCls, "getDeviceAddr", "(I)Ljava/lang/String;");
            var getDeviceNameByAddrMethod = AndroidJNI.GetMethodID(this.bleScannerCls, "getDeviceNameByAddr", "(Ljava/lang/String;)Ljava/lang/String;");
            var getRssiByAddrMethod       = AndroidJNI.GetMethodID(this.bleScannerCls, "getRssiByAddr", "(Ljava/lang/String;)I");

            AndroidJNI.CallVoidMethod(scanner, blitMethod, null);
            int num = AndroidJNI.CallIntMethod(scanner, getDeviceNumMethod, null);

            scannedDevices.Clear();
            for (int i = 0; i < num; ++i)
            {
                argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(i));
                string addr = AndroidJNI.CallStringMethod(scanner, getDeviceAddrMethod, argBuilder.Build());
                argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(addr));
                string name       = AndroidJNI.CallStringMethod(scanner, getDeviceNameByAddrMethod, argBuilder.Build());
                int    rssi       = AndroidJNI.CallIntMethod(scanner, getRssiByAddrMethod, argBuilder.Build());
                var    scanDevice = new BleScannedDevice(addr, name, rssi);
                this.scannedDevices.Add(scanDevice);
            }
            AndroidJNI.PopLocalFrame(IntPtr.Zero);
        }
Ejemplo n.º 5
0
        private System.IntPtr GetDeviceObj(string addr)
        {
            var getDeviceMethod = AndroidJNI.GetMethodID(bleManagerCls, "getDeviceByAddr", "(Ljava/lang/String;)Lcom/toio/ble/BleDeviceObj;");

            this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(addr));
            var deviceObj = AndroidJNI.CallObjectMethod(this.javaBleManagerObj, getDeviceMethod, this.argBuilder.Build());

            return(deviceObj);
        }
Ejemplo n.º 6
0
        public void Disconnect(string addr)
        {
            var disconnectMethod = AndroidJNI.GetMethodID(this.bleManagerCls,
                                                          "disconnect", "(Ljava/lang/String;)V");

            this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(addr));
            AndroidJNI.CallVoidMethod(this.javaBleManagerObj,
                                      disconnectMethod, this.argBuilder.Build());
        }
Ejemplo n.º 7
0
        public void StartScan(string uuid)
        {
            AndroidJNI.PushLocalFrame(32);
            var scanner    = GetScanner();
            var scanMethod = AndroidJNI.GetMethodID(this.bleScannerCls, "startScan", "(Ljava/lang/String;)V");

            this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(uuid));

            AndroidJNI.CallVoidMethod(scanner, scanMethod, this.argBuilder.Build());
            AndroidJNI.PopLocalFrame(IntPtr.Zero);
        }
Ejemplo n.º 8
0
        public void ReadCharacteristicRequest(string addr, string serviceUuid,
                                              string characteristicUUID)
        {
            var deviceObj         = GetDeviceObj(addr);
            var readrequestMethod = AndroidJNI.GetMethodID(bleDeviceCls, "readRequest",
                                                           "(Ljava/lang/String;Ljava/lang/String;)V");

            this.argBuilder.Clear().
            Append(ArgJvalueBuilder.GenerateJvalue(serviceUuid)).
            Append(ArgJvalueBuilder.GenerateJvalue(characteristicUUID));
            AndroidJNI.CallVoidMethod(deviceObj, readrequestMethod, this.argBuilder.Build());
        }
Ejemplo n.º 9
0
        public void SetNotificateFlag(string addr, string serviceUuid,
                                      string characteristicUUID, bool isEnable)
        {
            var deviceObj             = GetDeviceObj(addr);
            var setNotificationMethod = AndroidJNI.GetMethodID(bleDeviceCls, "setNotification",
                                                               "(Ljava/lang/String;Ljava/lang/String;Z)V");

            this.argBuilder.Clear().
            Append(ArgJvalueBuilder.GenerateJvalue(serviceUuid)).
            Append(ArgJvalueBuilder.GenerateJvalue(characteristicUUID)).
            Append(ArgJvalueBuilder.GenerateJvalue(isEnable));
            AndroidJNI.CallVoidMethod(deviceObj, setNotificationMethod, this.argBuilder.Build());
        }
Ejemplo n.º 10
0
        public void WriteCharacteristic(string addr,
                                        string serviceUuid,
                                        string characteristicUUID, byte[] data, int length,
                                        bool withResponse)
        {
            var deviceObj   = GetDeviceObj(addr);
            var writeMethod = AndroidJNI.GetMethodID(bleDeviceCls, "writeData",
                                                     "(Ljava/lang/String;Ljava/lang/String;[BZ)V");

            this.argBuilder.Clear().
            Append(ArgJvalueBuilder.GenerateJvalue(serviceUuid)).
            Append(ArgJvalueBuilder.GenerateJvalue(characteristicUUID)).
            Append(ArgJvalueBuilder.GenerateJvalue(data, length)).
            Append(ArgJvalueBuilder.GenerateJvalue(withResponse));
            AndroidJNI.CallVoidMethod(deviceObj, writeMethod, this.argBuilder.Build());
        }
Ejemplo n.º 11
0
        public void UpdateConnectedDevices()
        {
            this.readDatas.Clear();
            AndroidJNI.PushLocalFrame(32);
            var getConnectedDeviceNumMethod = AndroidJNI.GetMethodID(bleManagerCls,
                                                                     "getConnectedDeviceNum", "()I");
            var getConnectedDeviceMethod = AndroidJNI.GetMethodID(bleManagerCls,
                                                                  "getConnectedDevice", "(I)Lcom/toio/ble/BleDeviceObj;");
            int num = AndroidJNI.CallIntMethod(javaBleManagerObj, getConnectedDeviceNumMethod, null);

            for (int i = 0; i < num; ++i)
            {
                AndroidJNI.PushLocalFrame(32);
                this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(i));
                var device = AndroidJNI.CallObjectMethod(this.javaBleManagerObj, getConnectedDeviceMethod, argBuilder.Build());
                this.UpdateBleDevice(device);
                AndroidJNI.PopLocalFrame(IntPtr.Zero);
            }
            AndroidJNI.PopLocalFrame(IntPtr.Zero);
        }
Ejemplo n.º 12
0
        public void StartScan(string[] uuids)
        {
            AndroidJNI.PushLocalFrame(32);
            var scanner             = GetScanner();
            var startScanMethod     = AndroidJNI.GetMethodID(this.bleScannerCls, "startScan", "()V");
            var addScanFilterMethod = AndroidJNI.GetMethodID(this.bleScannerCls, "addScanFilter", "(Ljava/lang/String;)V");
            var clearScanMethod     = AndroidJNI.GetMethodID(this.bleScannerCls, "clearScanFilter", "()V");

            AndroidJNI.CallVoidMethod(scanner, clearScanMethod, null);

            if (uuids != null)
            {
                foreach (var uuid in uuids)
                {
                    this.argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(uuid));
                    AndroidJNI.CallVoidMethod(scanner, addScanFilterMethod, this.argBuilder.Build());
                }
            }

            AndroidJNI.CallVoidMethod(scanner, startScanMethod, null);
            AndroidJNI.PopLocalFrame(IntPtr.Zero);
        }
Ejemplo n.º 13
0
        public void Initialize()
        {
            this.argBuilder = new ArgJvalueBuilder();
            AndroidJNI.PushLocalFrame(32);
            var contxt = GetContext();

            this.bleManagerCls = GetGlobalRefClass("com/toio/ble/BleManagerObj");
            this.bleScannerCls = GetGlobalRefClass("com/toio/ble/BleScannerObj");
            this.bleDeviceCls  = GetGlobalRefClass("com/toio/ble/BleDeviceObj");
            var getInstanceMethod = AndroidJNI.GetStaticMethodID(bleManagerCls, "getInstance",
                                                                 "()Lcom/toio/ble/BleManagerObj;");

            var obj = AndroidJNI.CallStaticObjectMethod(bleManagerCls, getInstanceMethod, null);

            javaBleManagerObj = AndroidJNI.NewGlobalRef(obj);

            argBuilder.Clear().Append(ArgJvalueBuilder.GenerateJvalue(contxt.GetRawObject()));
            var initMethod = AndroidJNI.GetMethodID(bleManagerCls, "initialize", "(Landroid/content/Context;)V");

            AndroidJNI.CallVoidMethod(javaBleManagerObj, initMethod, argBuilder.Build());

            AndroidJNI.PopLocalFrame(IntPtr.Zero);
        }
Ejemplo n.º 14
0
        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);
        }