2016-02-14 200 views
3

美好的一天。我正在开发一个连接到BLE设备的Android应用程序,并读取Gatt服务和Gatt特性。我使用Android Development Site的BluetoothLeGatt示例项目作为参考。到目前为止,我能够以编程方式连接到设备(我注意到我的设备地址能够做到这一点),并通过记录UUID过滤出我想要阅读的特定Gatt服务以及该服务的特定特征服务和特征。 Google提供的样本也会在我的BLE设备发送到我的Android应用程序时更新。总的来说,我在这方面没有问题。Android BLE - 一次连接多个设备

但是,在进一步阅读关于GATT的文章后,我发现可以使用单个Android应用程序(作为主或客户端)连接到多个BLE设备(所有从设备或服务器 - 即发送数据的设备)作为接收所述数据的人)。所以我试图做的是拥有2个BLE设备(不同的地址),记下他们的地址,然后当应用程序看到这两个地址已启动并运行时,我的应用程序尝试连接到它们。

在代码中,我调用这个函数,当我看到我的2个BLE装置:

private void connectToDevice(){ 

    mDeviceName = deviceList.get(currentIndex).getName(); 
    mDeviceAddress = deviceList.get(currentIndex).getAddress(); 

    Log.e(TAG, "connecting to device name = " + mDeviceName); 

    mBluetoothLeService.connect(mDeviceAddress); 
} 

currentIndex最初设置为零。然后,一旦我得到一个成功的连接,我做的:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 
     if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 
      Log.e(TAG, "connected"); 
      mConnected = true; 

      if(currentIndex < deviceList.size()-1) currentIndex ++; 
      connectToDevice(); 

     } 
    } 
}; 

我在哪里检查,如果我仍然有设备连接到我的deviceList,如果是的话,增加我的柜台,然后连接,直到我用尽我的列表中的一切。

但是,我似乎没有成功使用这种方法。

请注意,我的设备之间的切换连接(循环)不是选项。这将是一个问题,因为我有很多设备,并且无延迟地实时传送消息非常重要。这就是说,我必须与我的设备进行实时连接。

有没有人试图连接到Android中的多个BLE设备?截至目前,我不确定如何进行这项工作,任何帮助或线索都非常感谢。

+0

是的,你可以和我做这使用一个简单的循环。我所做的是,首先我扫描设备,一旦发现我把它放在一个数组列表中并调用这个连接方法。要成功建立连接,您还需要锁定和等待机制,因为您想尝试在第一个连接之后建立第二个连接。 – Gautam

+0

@Gautam你会介意发布你的代码,使其工作?我很感谢这个话题的任何线索。 – Razgriz

回答

1

的确有可能从您的Android设备连接到多个peripheral。但是,它会使你的代码更加复杂,因为你需要管理每个连接和响应。

对于每个连接,您将必须实施BluetoothGatt与它的callbacks。我在几个月前用虚拟测试测试了它,正如我所说的那样,它运行良好,我可以连接到不同的外围设备。但是,如果链接了许多命令,则似乎存在this thread中描述的一些重叠问题。

+0

几个月前你会碰到你的代码吗?说实话,我所做的只是等待来自我的设备的消息,实际上我不需要发送消息,所以我不会担心链接命令。 – Razgriz

+0

不幸的是,对于一个工作项目来说这是一个快速的PoC,一旦我测试并证明了它,由于它的简单性,它直接去了垃圾箱。 – GoRoS

+0

那么我很幸运。你会记得,如果你为每个你必须连接的设备使用一个单独的'BluetoothLeService'?或者您是否只使用多个BluetoothGatt并分别处理回调? – Razgriz

0

如这里问的是相关代码:(这里的ArrayList包含的创办外围设备)

for(int i=0;i< Utility.selectedDeviceList.size();i++) { 
      Log.d(Utility.TAG,"state"+ Utility.selectedDeviceList.get(i).getmConnectionState()); 
      if (Utility.selectedDeviceList.get(i).getmConnectionState() != Utility.CONNECTED) { 
       Log.d(Utility.TAG,"Connecting LeSerive::" + Utility.selectedDeviceList.get(i).getAddress()); 
       Utility.mBluetoothLeService.connect(i, Utility.selectedDeviceList.get(i).getAddress()); 
      } 

     } 

这for循环Runnable接口被称为有弯针的处理程序中的一部分。

public void run() { 
    Looper.prepare(); 
    Looper mLooper = Looper.myLooper(); 
    Log.d(Utility.TAG,"BLE Thread Started::"); 
    mHandler = new Handler(mLooper) { 

     @Override 
     public void handleMessage(Message msg) { 

      switch (msg.what) { 
       case Utility.BLE_SYNC: 
        Log.d(Utility.TAG,"BLE Sync Connecting::"); 
        mHandler.post(SynState); 
        break; 
     } 
    }; 
    Looper.loop(); 
} 

我用这个方法,因为他们是很多外围设备之间的通信的发送和收到他们的数据。

这是连接方法,服务内:

public boolean connect(int tag,final String address) { 
    if (mBluetoothAdapter == null || address == null) { 
     Log.w(Utility.TAG, "BluetoothAdapter not initialized or unspecified address."); 
     return false; 
    } 

    Utility.selectedDeviceList.get(tag).setmConnectionState(Utility.CONNECTING); 

    if(Utility.selectedDeviceList.get(tag).getmBluetoothGatt()==null){ 
     Log.w(Utility.TAG, "new connect :: "+ Utility.selectedDeviceList.get(tag).getAddress()); 

     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
     if (device == null) { 
      Log.w(Utility.TAG, "Device not found. Unable to connect."); 
      return false; 
     } 
     try { 
      Utility.selectedDeviceList.get(tag).setmBluetoothGatt(device.connectGatt(this, false, mGattCallback)); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Log.d(Utility.TAG,"ConnectGatt exception caught"); 
     } 
    } 

    return true; 
} 

这是mGattCallBack:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 

    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 

    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     Log.d(Utility.TAG, "onServicesDiscovered"); 

    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status) { 


    } 
    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, 
             BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 
     Log.d(Utility.TAG,">>onCharacteristicWrite"); 

    } 
    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) { 

    } 
}; 

希望它可以清除一些事情你

+0

我试图做多个连接,处理程序和回调尝试连接到我的2设备同时。但是,它似乎并不奏效。我在这里发布了一个关于它的新问题:http://stackoverflow.com/questions/35406168/android-ble-connecting-to-multiple-devices-seem-to-fail-and-gatt-response-is-t and it如果你可以在那里看看我的代码会有帮助。谢谢! – Razgriz