更新:
在 OnCreate 中:
mChatService = new BluetoothChatService(mContext, mHandler);
for (BluetoothDevice device : MainActivity.mBluetoothAdapter.getBondedDevices()) {
mChatService.connect(device, false);
}
在该文件的后面:
protected void sendMessage(String s) throws IOException {
//outputStream.write(s.getBytes());
if(s.length() != 0) {
messagesAdapter.add(s);//UI On local device
messagesAdapter.notifyDataSetChanged();
mChatService.write(temp.getBytes());
messageTextbox.setText("");
Toast.makeText(mContext, "Message sent!", Toast.LENGTH_LONG).show();
}
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
Toast.makeText(mContext, "Connected(1)", Toast.LENGTH_SHORT).show();
break;
case BluetoothChatService.STATE_CONNECTING:
//setStatus(R.string.title_connecting);
Toast.makeText(mContext, "Connecting(1)", Toast.LENGTH_SHORT).show();
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
Toast.makeText(mContext, "Not Con(1)", Toast.LENGTH_SHORT).show();
break;
}
break;
case Constants.MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
break;
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(mContext, "NEW:" + readMessage, Toast.LENGTH_LONG).show();//------- This is where the message is received
if(!readMessage.equals("")){
messagesAdapter.add(readMessage);
}
//mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
//mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != mContext) {
Toast.makeText(mContext, "Connected to "
+ MainActivity.address, Toast.LENGTH_SHORT).show();
}
break;
case Constants.MESSAGE_TOAST:
if (null != mContext) {
Toast.makeText(mContext, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
}
break;
}
}
};
我不能说我已经在服务中尝试过,但您可以尝试将处理程序放入服务中。看看它是否仍然收到蓝牙消息。我知道这在活动中有效。
这是一个 example 从 Google 进行蓝牙聊天。包含显示如何查找附近设备、配对和在设备之间发送消息的代码。
在这个例子中,有一个DeviceListActivity 可以找到附近的设备:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 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);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
如您所见,这条线正在获取附近的设备名称和地址:
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
如果您想在应用未运行时运行代码,请查看 unbound services 和 another example。所以在 onDestroy 中你可以启动服务,在 onCreate 中停止服务。