[英]Connecting to a bluetooth device programmatically
我正在為新的車輛安全應用程序編寫程序。 該應用程序允許用戶通過其手機應用程序控制鎖定/解鎖操作。 假設用戶的手機藍牙最初是關閉的。 如果是這樣,當他打開應用程序時,手機藍牙適配器應該會自動打開,並且應該與固定在車輛上的藍牙模塊連接。 根據我所做的代碼,手機BT適配器的編程啟用工作正常。 但是與車輛 BT 模塊的連接不會發生。
但是如果用戶在手機藍牙適配器已經打開的情況下打開應用程序,則車輛和手機之間的連接會自動建立。
我需要知道為什么以編程方式打開 BT 適配器時不會發生連接。
注意 - 手機和車載藍牙模塊已配對。 藍牙模塊mac地址在編碼中是硬編碼的。 編碼如下。 我只粘貼了必要的部分。 我希望每個需要了解和解決我的問題的人都在這里。 我發布代碼的方式非常混亂。 對於那個很抱歉。 希望很清楚。 我是新手。
private static final UUID MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// Insert bluetooth devices MAC address
private static String address = "00:19:5D:EF:03:79";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btAdapter = BluetoothAdapter.getDefaultAdapter();
btAdapter.enable();
@Override
public void onResume() {
super.onResume();
btAdapter.enable();
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
// Make sure Discovery isn't going on when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}
可能存在計時問題,onCreate 和 onResume 的調用順序非常短。 在沒有開啟BT的情況下,可能會在BT服務上線前調用onResume中的代碼。
我的建議:嘗試通過將代碼放入 Runnable 來延遲啟動幾秒鍾。
private Handler mHandler = new Handler();
public void onCreate() {
[...]
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
btAdapter.enable();
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
// Make sure Discovery isn't going on when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}, 5000); // 5 second delay
[...]
警告:如果您在啟動后立即退出應用程序,這將非常糟糕。 將 runnable 放在成員變量中並在 onDestroy() 中調用 mHandler.removeCallback(Runnable)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.