我正在尝试从树莓派向Android应用发送消息。
pi上的代码如下:
os.system("systemctl start bluetooth")
os.system("hciconfig hci0 piscan")
time.sleep(0.5)
os.system("hciconfig hci0 name 'my raspberry'")
server_sock = bluetooth.BluetoothSocket(RFCOMM)
server_sock.listen(1)
...
uuid = "0000000......" # i am not showing the full uuid here
os.system("hcitool scan")
bluetooth.advertise_service(server_sock, "servername", service_id=uuid, service_classes= [uuid, SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE])
client_sock, client_info = server_sock.accept()
# this part is in a while loop which i remove in this snippet
data = ""
try :
data = client_sock.recv()
except IOError as e :
client_sock, client_info = server_sock.accept()
if data == "AAXX" :
client_sock.send("ANSWER")
Android端的代码:
private final BroadcastReceiver bluetoothStateChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_TURNING_OFF:
cancelBluetoothConnection();
break;
case BluetoothAdapter.STATE_ON:
startBluetoothConnection();
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
}
}
//Bundle info = intent.getExtras();
/*BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//String deviceAddress = device.getAddress();
if (action.equals( BluetoothDevice.ACTION_ACL_CONNECTED)){
bluetoothConnected = true;
Log.d("conncheck" , "got ACL COnnection");
//setConnectedUI(true);
}
if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
bluetoothConnected = false;
Log.d("conncheck" , "lost ACL COnnection");
//setConnectedUI(false);
}*/
}
};
private void checkBluetoothConnection() {
// If bluetooth not activated ask user for bluetooth permission
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
Intent bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(bluetoothIntent, ENABLE_BLUETOOTH_REQUEST);
} else {
// Start bluetooth connection
startBluetoothConnection();
}
}
private void startBluetoothConnection() {
RRSamplesApplication.startBluetoothConnection(bluetoothMessageReceivedListener);
// Check if still connected
if (!bluetoothConnectionHandlerIsRunning) {
bluetoothConnectionHandlerIsRunning = true;
final Handler checkConnectionStatusHandler = new Handler();
final int delay = 5000;
checkConnectionStatusHandler.postDelayed(new Runnable() {
@Override
public void run() {
checkConnectionStatus();
checkConnectionStatusHandler.postDelayed(this, delay);
}
}, delay);
}
}
private void cancelBluetoothConnection() {
if (RRSamplesApplication.getBluetoothThread() != null) {
RRSamplesApplication.getBluetoothThread().interrupt();
}
}
private BluetoothMessageReceivedListener bluetoothMessageReceivedListener = new BluetoothMessageReceivedListener() {
@Override
public void onBluetoothMessageReceived(final String message) {
Log.d("conncheck : ", message);
if (message.equals("ANSWER")) {
}
}
};
正在建立连接。但是随后,由于树莓不断尝试向应用发送消息,因此连接崩溃
File "<string>", line 5, in send
bluetooth.btcommon.BluetoothError: (104, 'Connection reset by peer')
如果我还将发送操作也放在try client_sock, client_info = server_sock.accept()/ except
块中,则它将尝试重新建立连接,有时可能成功,有时却不成功。
在做什么错了? ps:/etc/bluetooth/main.conf
`