我正在使用蓝牙聊天示例,它非常适合配对设备并将文本从一台设备发送到另一台设备但有时会断开连接,我想再次与最后连接的设备重新连接。我如何实现这一目标。我有尝试从Play商店自动连接蓝牙,但它连接耳机和应用程序外部,而不是从内部。
如何在App中实现这一目标?
先谢谢。
E/BluetoothChatService(10175): accept() failed
E/BluetoothChatService(10175): java.io.IOException: Operation Canceled
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:311)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:107)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:93)
E/BluetoothChatService(10175): at com.example.android.BluetoothChat.BluetoothChatService$AcceptThread.run(BluetoothChatService.java:276)
E/BluetoothChatService(10175): disconnected
E/BluetoothChatService(10175): java.io.IOException: Software caused connection abort
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.readNative(Native Method)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
E/BluetoothChatService(10175): at java.io.InputStream.read(InputStream.java:163)
E/BluetoothChatService(10175): at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:436)
E/AndroidRuntime(10175): FATAL EXCEPTION: Thread-1274
E/AndroidRuntime(10175): java.lang.NullPointerException
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService.connectionLost(BluetoothChatService.java:242)
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService.access$6(BluetoothChatService.java:221)
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:443)E/BluetoothChat(10175): - ON PAUSE -
答案 0 :(得分:3)
使用gEdit写这个,因为我没有eclipse atm。因此,如果有一些编码错误,请不要犹豫,编辑它。为了便于解释,我没有在这里使用SharedPreferences,如果你想使用,请不要犹豫。
//Assuming that you have device address and is connected
private String partnerDevAdd="00:11:22:AA:BB:CC";
private boolean isConnected=true;
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(mReceiver, filter);
// Create a BroadcastReceiver for bluetooth related checks
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//We don't want to reconnect to already connected device
if(isConnected==false){
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Check if the found device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
connectToExisting(device);
}
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Check if the connected device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
isConnected=true;
}else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Check if the connected device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
isConnected=false;
}
}
};
private void connectToExisting(BluetoothDevice device){
new ConnectThread(device);
}
此处提供了ConnectThread http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient