Exemple #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);
                        }
                    }
                }
            }
        }
Exemple #2
0
 private static void UpdateDisconnectedDevice()
 {
     if (!s_isInitialized)
     {
         return;
     }
     s_removeKeyBuffer.Clear();
     foreach (var kvs in s_deviceDiscoverEvents)
     {
         string identifier  = kvs.Key;
         var    addr        = DeviceAddressDatabase.GetAddressValue(identifier);
         var    discoverEvt = kvs.Value;
         if (!discoverEvt.callDiscoverEvent)
         {
             continue;
         }
         if (DllInterface.IsDeviceConnected(addr))
         {
             continue;
         }
         if (discoverEvt.disconnectedAct != null)
         {
             discoverEvt.disconnectedAct(identifier);
         }
         s_removeKeyBuffer.Add(identifier);
         //Debug.Log("DisconnectDevice " + identifier);
     }
     foreach (var key in s_removeKeyBuffer)
     {
         s_deviceDiscoverEvents.Remove(key);
     }
 }
Exemple #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);
            }
        }
Exemple #4
0
        private static void UpdateWriteRequests()
        {
            if (!s_isInitialized)
            {
                return;
            }
            s_removeIdxBuffer.Clear();
            int count = s_writeRequests.Count;

            for (int i = 0; i < count; ++i)
            {
                var request = s_writeRequests[i];
                if (DllInterface.IsWriteRequestComplete(request.handle))
                {
                    if (request.didWriteCharacteristicAction != null)
                    {
                        request.didWriteCharacteristicAction(request.charastericsInfo.serviceUUID, request.charastericsInfo.characteristicUUID);
                    }
                    s_removeIdxBuffer.Add(i);
                }
                else if (DllInterface.IsWriteRequestError(request.handle))
                {
                    s_removeIdxBuffer.Add(i);
                }
            }
            // remove done request
            s_removeIdxBuffer.Reverse();
            foreach (var idx in s_removeIdxBuffer)
            {
                var request = s_writeRequests[idx];
                var addr    = DeviceAddressDatabase.GetAddressValue(request.charastericsInfo.address);
                DllInterface.ReleaseWriteRequest(addr, request.handle);
                s_writeRequests.RemoveAt(idx);
            }
        }
Exemple #5
0
        private static void UpdateScanDeviceEvents()
        {
            if (!s_isInitialized)
            {
                return;
            }
            // update scan devices
            int scanNum = DllInterface.ScanGetDeviceLength();

            // Debug.Log("UpdateScanDeviceEvents " + scanNum);
            for (int i = 0; i < scanNum; ++i)
            {
                ulong addr       = DllInterface.ScanGetDeviceAddr(i);
                var   identifier = DeviceAddressDatabase.GetAddressStr(addr);
                //Debug.Log("UpdateScanDeviceEvents identifier " + identifier);
                var name = "";// DllInterface.ScanGetDeviceName(i);
                //Debug.Log("UpdateScanDeviceEvents name " + name);
                var rssi = DllInterface.ScanGetDeviceRssi(i);
                //Debug.Log("UpdateScanDeviceEvents rssi " + rssi);
                if (s_discoverAction != null)
                {
                    s_discoverAction(identifier, name, rssi, null);
                }
            }
        }
Exemple #6
0
        public static void ConnectToPeripheral(string identifier,
                                               Action <string> connectedPeripheralAction       = null,
                                               Action <string, string> discoveredServiceAction = null,
                                               Action <string, string, string> discoveredCharacteristicAction = null,
                                               Action <string> disconnectedPeripheralAction = null)
        {
            if (!s_isInitialized)
            {
                return;
            }
            //Debug.Log("Connect to peripheral " + identifier);
            if (s_deviceDiscoverEvents.ContainsKey(identifier))
            {
                return;
            }
            var addr = DeviceAddressDatabase.GetAddressValue(identifier);

            DllInterface.ConnectDevice(addr);
            var evt = new BleDiscoverEvents(connectedPeripheralAction,
                                            discoveredServiceAction,
                                            discoveredCharacteristicAction,
                                            disconnectedPeripheralAction);

            s_deviceDiscoverEvents.Add(identifier, evt);
        }
Exemple #7
0
        private static void UpdateDeviceConnectEvents()
        {
            if (!s_isInitialized)
            {
                return;
            }
            s_allreadyCallServiceBuffer.Clear();
            // update connect Event
            int deviceNum = DllInterface.GetConnectDeviceNum();

            //Debug.Log("UpdateDeviceConnectEvents " + deviceNum);
            for (int i = 0; i < deviceNum; ++i)
            {
                var               addr       = DllInterface.GetConnectDeviceAddr(i);
                string            identifier = DeviceAddressDatabase.GetAddressStr(addr);
                BleDiscoverEvents bleDiscoverEvents;
                if (!s_deviceDiscoverEvents.TryGetValue(identifier, out bleDiscoverEvents))
                {
                    continue;
                }
                if (bleDiscoverEvents.callDiscoverEvent)
                {
                    continue;
                }
                if (bleDiscoverEvents.connectedAct != null)
                {
                    bleDiscoverEvents.connectedAct(identifier);
                }

                var deviceHandle = DllInterface.GetConnectDevicePtr(i);
                int chNum        = DllInterface.GetDeviceCharastricsNum(deviceHandle);
                for (int j = 0; j < chNum; ++j)
                {
                    var serviceHandle  = DllInterface.GetDeviceCharastricServiceUuid(deviceHandle, j);
                    var charaHandle    = DllInterface.GetDeviceCharastricUuid(deviceHandle, j);
                    var serviceUuidStr = UuidDatabase.GetUuidStr(serviceHandle);
                    var charaUuidStr   = UuidDatabase.GetUuidStr(charaHandle);

                    if (!s_allreadyCallServiceBuffer.Contains(serviceUuidStr))
                    {
                        if (bleDiscoverEvents.discoveredServiceAct != null)
                        {
                            bleDiscoverEvents.discoveredServiceAct(identifier, serviceUuidStr);
                        }
                        s_allreadyCallServiceBuffer.Add(serviceUuidStr);
                    }
                    if (bleDiscoverEvents.discoveredCharacteristicAct != null)
                    {
                        bleDiscoverEvents.discoveredCharacteristicAct(identifier, serviceUuidStr, charaUuidStr);
                    }
                }

                bleDiscoverEvents.callDiscoverEvent = true;
            }
        }
Exemple #8
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);
        }
Exemple #9
0
        public static void DisconnectPeripheral(string identifier,
                                                Action <string> disconnectedPeripheralAction = null)
        {
            if (!s_isInitialized)
            {
                return;
            }
            //Debug.Log("DisconnectPeripheral " + identifier);

            var addr = DeviceAddressDatabase.GetAddressValue(identifier);

            DllInterface.DisconnectDevice(addr);
        }
Exemple #10
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);
        }
Exemple #11
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);
        }