android 怎么根据mac地址断开蓝牙连接
发布网友
发布时间:2022-04-29 15:40
我来回答
共1个回答
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接
热心网友
时间:2023-10-16 07:02
对于单片机蓝牙连接的过程中,搜索过程虽然省略了,但是连接仍然是费时间的,所有我把它单独写了一个进程来实现,以免出现黑屏的现象,同时程序里我还对bundle传值,Activity间跳转方式进行了修改。
/*
开启多线程实现蓝牙连接的耗时操作
*/
private class ConnectThread implements Runnable {
@Override
public void run() {
Message message = new Message();
BluetoothDevice device = _bluetooth.getRemoteDevice(address);
try {
_socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException e1) {
e1.printStackTrace();
}
Log.e("ceshi", "ON RESUME: Socket creation failed.", e);
}
_bluetooth.cancelDiscovery();
try {
_socket.connect();
message.what = SUCCESS;
} catch (IOException e) {
try {
message.what = FAIL;
_socket.close();
_socket = null;
} catch (IOException ee) {
message.what = FAIL;
}
return;
}
connectHandler.sendMessage(message);
}
}
/*
通过handler接收蓝牙连接状态信息
*/
private class ConnectHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 0:
myToast(R.string.link_fail);
break;
case 1:
Toast.makeText(getApplicationContext(), "连接" + addressName + "||" + address + "成功!", Toast.LENGTH_SHORT).show();
break;
case 2:
myToast(R.string.not_found_bluetooth);
}
}
}追问我不是要连接 我是连接到这个蓝牙后 发送数据过后,断开这个蓝牙连接