[英]Printing a list of available bluetooth devices
我試圖在打開活動時使我的應用程序打印可用藍牙設備的列表。
首先,我在設備上啟用藍牙:
public class Home extends AppCompatActivity {
TextView textView3;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView3 = (TextView) findViewById(R.id.textView3);
if (btAdapter == null) {
textView3.append("\nBluetooth not supported. Aborting.");
}
}
然后我嘗試將找到的所有設備添加到適配器
private final BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//add device to the adapter
btAdapter.add(device.getName(), device.getAddress());
}
}
};
我知道某個地方應該有一個startDiscovery()調用,但是我無法在線理解其他答案。 另外,由於某些原因無法識別我的btAdapter.add()。
任何幫助,將不勝感激!
由於某些原因無法識別我的btAdapter.add()。
因為BluetoothAdapter中沒有這樣的方法。
我知道某個地方應該有一個startDiscovery()調用,但是我無法在線理解其他答案。
一旦您確定設備支持藍牙並且從DeviceSettings中打開了藍牙,則可以調用startDiscovery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.