简体   繁体   English

自动连接到Android上预定的蓝牙配对设备

[英]Automatically connect to predetermined bluetooth paired device on Android

I have an application which connects to a bluetooth device, opens an input/outputstream and a socket and functions as some kind of "chat" between these two devices. 我有一个连接到蓝牙设备,打开输入/输出流和套接字并在这两个设备之间充当某种“聊天”功能的应用程序。 The chat part and connection and everything is done. 聊天部分和连接,一切都已完成。

However I've run into quite a wall here, I need to add an option so that when the app connects to a bluetooth device it gives you the option to save it as the default bluetooth device, so when you first open the app, if the default device is in range it automatically connects to it (the device is already paired). 但是我在这里碰壁了,我需要添加一个选项,以便当应用程序连接到蓝牙设备时,它为您提供了将其另存为默认蓝牙设备的选项,因此,当您首次打开该应用程序时,如果默认设备在范围之内,它会自动连接到该设备(该设备已配对)。

So basically what I need is: We connect to a (already paired) bluetooth device, we answer yes to saving it as a default device and now whenever the app is opened it should automatically look for and connect to this device if it is in range. 所以基本上我需要的是:我们连接到(已配对)蓝牙设备,对将其保存为默认设备回答是,现在只要打开应用程序,它就会自动寻找并连接到该设备(如果它在范围内) 。

info from: http://developer.android.com/guide/topics/connectivity/bluetooth.html 信息来自: http : //developer.android.com/guide/topics/connectivity/bluetooth.html

 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
 // If there are paired devices
      if (pairedDevices.size() > 0) {
     // Loop through paired devices
     for (BluetoothDevice device : pairedDevices) {
    // Add the name and address to an array adapter to show in a ListView
         mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM