如何在 Android Studio 中的应用启动时连接配对的蓝牙设备?

huangapple go评论77阅读模式
英文:

How to connect paired bluetooth device on app startup in Android Studio?

问题

以下是你要求的翻译内容:

有没有办法在应用启动时通过蓝牙LE自动连接特定设备?

我已经在过去的几个小时里在Stack Overflow上浏览了一些内容,看到了许多类似的问题,尽管大部分都相当旧,涉及到反射或其他我无法完全理解的复杂方法(我已经尝试实现这些方法,但并没有成功,因为我真的不明白其中发生了什么)。

到目前为止,我已经成功通过友好名称找到了设备,尽管我不知道在那个if语句中应该执行什么。这是在我的MainActivity中:

protected void onCreate(Bundle savedInstanceState) {
    ...
    if (bluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(),"不支持蓝牙",Toast.LENGTH_SHORT).show();

    } else {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size()>0){
            for(BluetoothDevice device: pairedDevices){

                if (deviceName.equals(device.getName())) {

                    //找到设备!
                    //现在我该如何配对它?

                    break;
                }
    ...

注意:在翻译过程中,我保留了代码中的注释和标记(如///*...*/),并进行了相应的翻译。

英文:

Is there any way to automatically connect a specific device via Bluetooth LE on app startup?

I've been scrolling through stack overflow for the past few hours and have seen a number of similar questions, although majority are quite outdated and deal with reflections or other complex methods that I can't quite comprehend (these methods I've tried to implement, but not successfully, as I didn't really understand what was going on).

So far, I've managed to find the device by its friendly name, although I have no clue what to execute in that if statement. This is within my MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    ...
    if (bluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(),&quot;Bluetooth not supported&quot;,Toast.LENGTH_SHORT).show();

    } else {
        Set&lt;BluetoothDevice&gt; pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size()&gt;0){
            for(BluetoothDevice device: pairedDevices){

                if (deviceName.equals(device.getName())) {

                    //Device found!
                    //Now how do I pair it?

                    break;
                }
    ...

答案1

得分: 0

假设您已成功识别出 BlueToothDevice,现在您需要连接到 GATT(通用属性配置文件),以便进行数据传输。
<br>
<br>
使用 BlueToothDevice.connectGatt 方法。使用第一个重载,该方法接受一个 Context、一个布尔值(false = 直接连接,true = 在可用时连接),以及一个 BlueToothGattCallback。回调函数从设备接收信息。

BlueToothGatt blueToothGatt = device.connectGatt(this, false, blueToothGattCallback);

以下是实现回调的示例代码:

BluetoothGattCallback blueToothGattCallback =
   new BluetoothGattCallback() 
   {
       @Override
       public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          if(newState == BlueToothProfile.STATE_CONNECTED){ 
              /* 做一些操作 */
          }
       }

   }

有关回调的更多详细信息,请参阅此处

英文:

Assuming you've successfully identified the BlueToothDevice, you now need to connect to the GATT(Generic Attribute Profile), which allows you to transfer data.
<br>
<br>
Use the BlueToothDevice.connectGatt method. Using the first overload, the method takes in a Context , a boolean (false = directly connect, true = connect when available), and a BlueToothGhattCallback. The callback receives info from the device.

BlueToothGatt blueToothGatt = device.connectGatt(this, false, blueToothGattCallback);

An example to implement the callback:

BluetoothGattCallback blueToothGattCallback =
   new BluetoothGattCallback() 
   {
       @Override
       public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          if(newState == BlueToothProfile.STATE_CONNECTED){ 
              /* do stuff */
          }
       }

   }

More details on the callbacks here.

答案2

得分: 0

浏览了此应用程序的源代码,特别是SerialSocket、SerialService和SerialListener文件,完全解决了我的问题。

英文:

Ended up scrolling through the source code for this app, particularly the SerialSocket, SerialService and SerialListener files which completely solved my problem.

huangapple
  • 本文由 发表于 2020年9月4日 22:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定