Beispiel #1
0
        private static void UpdateNotification()
        {
            if (!s_isInitialized)
            {
                return;
            }
            int deviceNum = DllInterface.GetConnectDeviceNum();

            for (int i = 0; i < deviceNum; ++i)
            {
                ulong  addr       = DllInterface.GetConnectDeviceAddr(i);
                string identifier = DeviceAddressDatabase.GetAddressStr(addr);

                int num = DllInterface.GetDeviceNotificateNum(addr);
                //Debug.Log("UpdateNotification " + identifier + "::" +num + "  " + i + "/" + deviceNum );
                for (int j = 0; j < num; ++j)
                {
                    var    data               = DllInterface.GetDeviceNotificateData(addr, j);
                    var    serviceHandle      = DllInterface.GetDeviceNotificateServiceUuid(addr, j);
                    var    charastricHandle   = DllInterface.GetDeviceNotificateCharastricsUuid(addr, j);
                    string serviceUUID        = UuidDatabase.GetUuidStr(serviceHandle);
                    string characteristicUUID = UuidDatabase.GetUuidStr(charastricHandle);

                    var           key = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);
                    BleNotifyData bleNotifyData;
                    if (s_notifyEvents.TryGetValue(key, out bleNotifyData))
                    {
                        if (bleNotifyData.notifiedCharacteristicAction != null)
                        {
                            bleNotifyData.notifiedCharacteristicAction(serviceUUID, characteristicUUID, data);
                        }
                    }
                }
            }
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public static void WriteCharacteristic(string identifier,
                                               string serviceUUID, string characteristicUUID,
                                               byte[] data, int length, bool withResponse,
                                               Action <string, string> didWriteCharacteristicAction)
        {
            if (!s_isInitialized)
            {
                return;
            }
            //Debug.Log("WriteCharacteristic " + identifier);
            var addr = DeviceAddressDatabase.GetAddressValue(identifier);

            var serviceHandle        = UuidDatabase.GetUuid(serviceUUID);
            var characteristicHandle = UuidDatabase.GetUuid(characteristicUUID);
            var writeRequest         = DllInterface.WriteCharastristicRequest(addr, serviceHandle, characteristicHandle, data, 0, length);

            if (withResponse && didWriteCharacteristicAction == null)
            {
                var charastricsItem = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);
                var requestData     = new BleWriteRequestData(charastricsItem, writeRequest, didWriteCharacteristicAction);
                s_writeRequests.Add(requestData);
            }
            else
            {
                DllInterface.ReleaseWriteRequest(addr, writeRequest);
            }
        }
Beispiel #4
0
        public static void SubscribeCharacteristic(string identifier,
                                                   string serviceUUID, string characteristicUUID,
                                                   Action <string, string, byte[]> notifiedCharacteristicAction)
        {
            //Debug.Log("SubscribeCharacteristic " + identifier + ":" + serviceUUID + ":" + characteristicUUID);
            var addr                 = DeviceAddressDatabase.GetAddressValue(identifier);
            var serviceHandle        = UuidDatabase.GetUuid(serviceUUID);
            var characteristicHandle = UuidDatabase.GetUuid(characteristicUUID);
            var charastricsItem      = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);

            s_notifyEvents[charastricsItem] = new BleNotifyData(notifiedCharacteristicAction);
            DllInterface.SetNotificationRequest(addr, serviceHandle, characteristicHandle, true);
        }
Beispiel #5
0
        private static BleDeviceDataEvents GetDataEvent(string identifier,
                                                        string serviceUUID, string characteristicUUID)
        {
            BleCharastericsKeyInfo key  = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);
            BleDeviceDataEvents    data = null;

            if (s_deviceDataEvents.TryGetValue(key, out data))
            {
                return(data);
            }
            data = new BleDeviceDataEvents();
            s_deviceDataEvents.Add(key, data);
            return(data);
        }
Beispiel #6
0
        public static void UnSubscribeCharacteristic(string identifier,
                                                     string serviceUUID, string characteristicUUID,
                                                     Action <string> action)
        {
            if (!s_isInitialized)
            {
                return;
            }
            //Debug.Log("UnSubscribeCharacteristic " + identifier + ":" + serviceUUID + ":" + characteristicUUID);
            var addr                 = DeviceAddressDatabase.GetAddressValue(identifier);
            var serviceHandle        = UuidDatabase.GetUuid(serviceUUID);
            var characteristicHandle = UuidDatabase.GetUuid(characteristicUUID);
            var charastricsItem      = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);

            DllInterface.SetNotificationRequest(addr, serviceHandle, characteristicHandle, false);
            s_notifyEvents.Remove(charastricsItem);
        }
Beispiel #7
0
        public static void ReadCharacteristic(string identifier,
                                              string serviceUUID,
                                              string characteristicUUID,
                                              Action <string, string, byte[]> didReadChracteristicAction)
        {
            if (!s_isInitialized)
            {
                return;
            }
            //Debug.Log("ReadCharacteristic " + identifier);
            var addr                 = DeviceAddressDatabase.GetAddressValue(identifier);
            var serviceHandle        = UuidDatabase.GetUuid(serviceUUID);
            var characteristicHandle = UuidDatabase.GetUuid(characteristicUUID);
            var charastricsItem      = new BleCharastericsKeyInfo(identifier, serviceUUID, characteristicUUID);

            var readRequestHandle = DllInterface.ReadCharastristicRequest(addr, serviceHandle, characteristicHandle);
            var requestData       = new BleReadRequestData(charastricsItem, readRequestHandle, didReadChracteristicAction);

            s_readRequests.Add(requestData);
        }
Beispiel #8
0
        private static void UpdateDeviceData()
        {
            // read/notify data
            var readDatas = javaWrapper.GetCharacteristicDatas();

            foreach (var readData in readDatas)
            {
                var key = new BleCharastericsKeyInfo(readData.deviceAddr, readData.serviceUuid, readData.characteristic);
                BleDeviceDataEvents dataEvt = null;
                if (!s_deviceDataEvents.TryGetValue(key, out dataEvt))
                {
                    Debug.LogError("Not Found key");
                    continue;
                }
                if (readData.isNotify)
                {
                    dataEvt.CallNotify(readData.serviceUuid, readData.characteristic, readData.data);
                }
                else
                {
                    dataEvt.CallRead(readData.serviceUuid, readData.characteristic, readData.data);
                }
            }
        }
Beispiel #9
0
        private static void UpdateDeviceFoundEvents()
        {
            // charastric / service found Infos
            var services = new HashSet <string>();

            var charstricInfoByDevice = javaWrapper.GetCharastricKeyInfos();

            foreach (var kvs in s_deviceDiscoverEvents)
            {
                string addr        = kvs.Key;
                var    deviceEvent = kvs.Value;
                List <BleCharastericsKeyInfo> charstricInfos = null;

                if (charstricInfoByDevice.TryGetValue(addr, out charstricInfos))
                {
                    if (!deviceEvent.callDiscoverEvent)
                    {
                        // connected
                        deviceEvent.connectedAct(addr);

                        // callback discover service
                        BleCharastericsKeyInfo.GetServices(services, charstricInfos);
                        foreach (var service in services)
                        {
                            deviceEvent.discoveredServiceAct(addr, service);
                        }
                        // callback discover service
                        foreach (var chInfo in charstricInfos)
                        {
                            deviceEvent.discoveredCharacteristicAct(chInfo.address, chInfo.serviceUUID, chInfo.characteristicUUID);
                        }
                        deviceEvent.callDiscoverEvent = true;
                    }
                }
            }
        }