[英]Android bluetooth connect to paired device
目前,我有一個要通過藍牙連接的設備(用於Arduino的藍牙模塊)。 但是每次我嘗試連接時,都不會發生任何事情。 有人可以告訴我我做錯了嗎? 我的代碼:
private static final UUID CONNUUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
public void connectDevice(BluetoothDevice bd){
try{
pairDia = ProgressDialog.show(this, "", "Connecting...", true, true);
BluetoothSocket bs = bd.createInsecureRfcommSocketToServiceRecord(CONNUUID);
}catch(Exception e){
e.printStackTrace();
this.finish();
}
}
}
最終,我想連接到設備,然后為它創建一個套接字,然后可以讀寫字節。 謝謝
您忘記了在BluetoothSocket
上調用connect()
:
// ...
BluetoothSocket bs = bd.createInsecureRfcommSocketToServiceRecord(CONNUUID);
bs.connect(); // note: blocking call
// ...
如果您的設備已配對。 然后首先使用以下命令獲取設備UUID
:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
// now tm.getdeviceID()...and is this equal to your CONNUUID?
在清單中設置藍牙許可....,然后通過以下命令查看設備列表:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
String sDeviceName = device.getName().trim();
Log.d("device_found", sDeviceName);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.