2014-07-22 136 views
0

我有一個應用程序連接到藍牙設備,打開輸入/輸出流和套接字,並作爲某種「聊天」在這兩個設備之間的功能。 聊天部分和連接,一切都完成了。自動連接到Android上預定的藍牙配對設備

但是我在這裏碰到了一堵牆,我需要添加一個選項,以便當應用程序連接到藍牙設備時,它會提供將其保存爲默認藍牙設備的選項,因此當您第一次打開應用程序,如果默認設備在範圍內,它會自動連接到它(設備已經配對)。

所以基本上我需要的是:我們連接到一個(已經配對的)藍牙設備,我們回答是將它保存爲默認設備,現在無論何時應用程序打開,它應該自動查找並連接到此設備if它在範圍內。

回答

0

信息來源: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()); 
    } 
}