9

मैं एक से अधिक BLE विशेषता का उपयोग कर Xamarin/Android के लिए सूचनाएं सक्षम करने की कोशिश कर रहा हूं, लेकिन ऐसा करने में असमर्थ लग रहे हैं। यदि मैं एक समय में एक से अधिक कोशिश करता हूं और सक्षम करता हूं तो ऐप किसी भी बीएलई ईवेंट प्राप्त करना बंद कर देता है।ज़ैमरिन/एंड्रॉइड पर एकाधिक बीएलई विशेषता अधिसूचनाओं को कैसे सक्षम करें?

किसी को भी इस बात की पुष्टि कर सकते हैं कि क्या यह टामारिन/Android का उपयोग संभव है। हमारे पास एक मूल आईओएस ऐप है जो कई नोटिफिकेशन सक्षम के साथ ठीक काम करता है। बुनियादी कदम उपयोग हम इस प्रकार हैं:

  1. डिवाइस
  2. के लिए स्कैन
  3. कनेक्ट डिवाइस के लिए
  4. डिस्कवर सेवाओं
  5. विशेषताओं के माध्यम से एक खोज की सेवा पुनरावृति के लिए और लोगों को कि आवश्यक हैं
  6. प्रक्रिया सक्षम बीएलई कॉलबैक

में प्रत्येक एसिंक्रोनस कॉलबैक ईवेंट जब भी हम अधिसूचनाओं को अधिक से अधिक कोशिश करते हैं और सक्षम करते हैं एक विशेषता हम अब किसी भी घटना प्राप्त नहीं करते हैं।

मैं भी किसी भी उदाहरण हैं, जहां एक से अधिक विशेषता सक्षम किया जा रहा है खोजने में असमर्थ किया गया है।

मुझे आशा है कि मैं बस कुछ यहाँ Xamarin/एंड्रॉयड एपीआई उपयोग के बारे में मौलिक छूट गई है।

public override void OnServicesDiscovered (BluetoothGatt gatt, GattStatus status) 
{ 
    base.OnServicesDiscovered (gatt, status); 
    foreach (BluetoothGattService service in gatt.Services) { 
     string uuid = service.Uuid.ToString().ToUpper(); 
     if (uuid.Equals (BLEServices.HRService.ToUpper())) { 
      _Adap.LogMessage ("HRService discovered"); 
      foreach(BluetoothGattCharacteristic characteristic in service.Characteristics) { 
       string c_uuid = characteristic.Uuid.ToString().ToUpper(); 
       _Adap.LogMessage (" HRCharacteristic: " + c_uuid); 

       if (c_uuid.Equals(_Adap.useCharacteristic.ToUpper())) { 
        _Adap.LogMessage (" enabling HRCharacteristic"); 
        gatt.SetCharacteristicNotification(characteristic, true); 
        BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read); 
        characteristic.AddDescriptor (descriptor); 
        descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray()); 
        gatt.WriteDescriptor (descriptor); 
        _Adap.StartTimer(); 
       } 
      } 

     } else if (uuid.Equals (BLEServices.BatteryService.ToUpper())) { 
      _Adap.LogMessage ("BatteryService discovered"); 
      foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) { 
       string c_uuid = characteristic.Uuid.ToString().ToUpper(); 
       _Adap.LogMessage (" BatteryService: " + c_uuid); 

       if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper())) { 
        _Adap.LogMessage (" reading batteryCharacteristic"); 
        // This may only be reported when the battery level changes so get the level first by doing a read 
        gatt.ReadCharacteristic (characteristic); 

        //gatt.SetCharacteristicNotification (characteristic, true); 
        //BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read); 
        //characteristic.AddDescriptor (descriptor); 
        //descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray()); 
        //gatt.WriteDescriptor (descriptor); 
       } 
      } 
     } else if (uuid.Equals (BLEServices.DeviceInfoService.ToUpper())) { 
      _Adap.LogMessage ("DeviceInfoService discovered"); 
      foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) { 
       string c_uuid = characteristic.Uuid.ToString().ToUpper(); 
       _Adap.LogMessage (" DeviceInfoService: " + c_uuid); 
       if (c_uuid.Equals (BLEServices.kModelNumberCharacteristicUuidString.ToUpper())) { 
        //gatt.ReadCharacteristic (characteristic); 
       } 
      } 
     } else if (uuid.Equals (BLEServices.kHxM2CustomServiceUuidString.ToUpper())) { 
      _Adap.LogMessage ("HxM2CustomService discovered"); 
      foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) { 
       string c_uuid = characteristic.Uuid.ToString().ToUpper(); 
       _Adap.LogMessage (" HxM2CustomCharacteristic: " + c_uuid); 

       if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper())) { 
        _Adap.LogMessage (" enabling HxM2 characteristic: "+_Adap.useCharacteristic); 
        gatt.SetCharacteristicNotification (characteristic, true); 
        BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read); 
        characteristic.AddDescriptor (descriptor); 
        descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray()); 
        gatt.WriteDescriptor (descriptor); 
        // Start a timer to make sure that we can recover if we never receive any data from the device 
        _Adap.StartTimer(); 
       } 

      } 
     } else { 
      _Adap.LogMessage ("Unknown Service "+uuid+" discovered"); 
     } 
    } 
} 

किसी को भी व्याख्या कर सकते हैं क्या निम्नलिखित लाइनों अपने पाया समाधान के अलावा के लिए

BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read); 
characteristic.AddDescriptor (descriptor); 
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray()); 
gatt.WriteDescriptor (descriptor); 
+1

अंत में मैं कुछ है कि काम करता है पाया। com/प्रश्न/17910322/android-ble-api-गैट-सूचना-नहीं-प्राप्त http://stackoverflow.com/questions/21278993/android-ble-how-to-read-multiple-characteristics –

उत्तर

2

हैं: ध्यान रखें, कि आप विशेषताओं में से एक असीमित संख्या में बात नहीं कर सकते हैं। अधिकतम एंड्रॉइड स्रोत में BTA_GATTC_NOTIF_REG_MAX पर हार्डकोड किया गया है।

तो अपने अनुप्रयोग की तुलना में अधिक पर भरोसा नहीं करना चाहिए आपके न्यूनतम समर्थित एंड्रॉइड संस्करण की विशेषताओं की अधिसूचना की अधिकतम संख्या।

0

वहाँ उपलब्ध फिर से विकल्प की संख्या है, लेकिन मैं इस विशेषताओं के लिए लागू करने के लिए तर्क नीचे करने के लिए इस्तेमाल किया और यह वास्तव में कृपया यह कोशिश काम कर रहा होगा: // stackoverflow: http:

public ConnectThread(BluetoothDevice device, BluetoothDeviceConnector deviceConnector) 
      { 
       _mmDevice = device; 
       BluetoothSocket tmp = null; 
       _deviceConnector = deviceConnector; 

       Log.Info(Tag, "calling device.createRfcommSocket with channel 1 ..."); 
       try 
       { 
        //tmp = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB")); 
        //tmp = device.CreateRfcommSocketToServiceRecord(device.GetUuids()[0].Uuid); 
        deviceConnector.BluetoothAdapter.CancelDiscovery(); 

        var createRfcommSocket = JNIEnv.GetMethodID(device.Class.Handle, "createInsecureRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;"); 
        var socket = JNIEnv.CallObjectMethod(device.Handle, createRfcommSocket, new JValue(1)); 
        tmp = GetObject<BluetoothSocket>(socket, JniHandleOwnership.TransferLocalRef); 

        var uuidList = device.GetUuids(); 
        if (uuidList != null) 
        { 
         foreach (var uuid in uuidList) 
         { 
          try 
          { 
           tmp = device.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid); 
           tmp.Connect(); 
           if (tmp.IsConnected) 
            break; 
          } 
          catch (Exception) 
          { 
           // ignored 
          } 
         } 
        } 

        Log.Info(Tag, "setting socket to result of createRfcommSocket"); 
       } 
       catch (Exception e) 
       { 
        Log.Error(Tag, e.Message, e); 
       } 
       _mmSocket = tmp; 
      } 
संबंधित मुद्दे