Bluetooth Automatic Connection with Paired Devices
I am new here and I have read a lot of your post, and still don't find the solution of开发者_开发知识库 my problem.
I am writing an App for Android 2.2 using Bluetooth to connect to an End-Device. I have a list of Paired Devices and I can connect my Android-Tablet with each of my already known devices.
What I want to do, is to connect with an End-Device automatically as soon as the Android-Tablet (Master in the whole communication, by the way) detects that one of the known Paired-Devices is in range.
One possibility is to constantly poll and try to see who is near me, but that would cost a lot of battery life, and if I go in range with one of the End-Devices and my Android-Tablet is not in the middle of the Polling-Process, I would not get the automatic connection; I would have to wait until the next Polling-Cycle.
Is there any solution to the problem?
I would the whole thing to work like the BT-Headsets and my handy :-/
Thanks for your answers and hope we can deal with it!
I am not sure whether this solution works or not. The idea is get all the paired devices and loop through it and try to connect using the MAC address of that device
String macAddress;
for (BluetoothDevice device : pairedDevices) {
BluetoothSocket bluetoothSocket = null;
try {
if (bluetoothSocket == null || !bluetoothSocket.isConnected()) {
bluetoothSocket = device.createRfcommSocketToServiceRecord(MYUUID);
mBluetoothAdapter.cancelDiscovery();
if(!bluetoothSocket.isConnected()){
bluetoothSocket.connect();
}
if (bluetoothSocket.getInputStream() != null && bluetoothSocket.getOutputStream() != null) {
macAddress = device.getAddress();
}
}
精彩评论