服务器端的等待响应以及链接反馈
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "蓝牙不可用", Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
bluetoothStateChanged(BluetoothStatusReceiver.BLUETOOTH_OFF);
}
setContentView(R.layout.activity_server);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mStartFab = (Button) findViewById(R.id.fab);
// mStartFab.hide();
mStartFab.setVisibility(View.GONE);
mStartFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRecyclerAdapter.getNumSockets(ServerAdapter.ACCEPTED) == 0) {
Toast.makeText(ServerActivity.this, "不接受用户", Toast.LENGTH_SHORT).show();
return;
}
for (BluetoothSocket socket : mRecyclerAdapter.getSockets(ServerAdapter.UNACCEPTED))
mBinder.removeSocket(socket.getRemoteDevice().getAddress(), BluetoothServiceUtility.CLOSE_KICKED_FROM_SERVER);
mBinder.setHandler(null);
Intent intent = new Intent(ServerActivity.this, ChatroomActivity.class);
intent.putExtra(ChatroomActivity.EXTRA_SERVER, true);
startActivity(intent);
}
});
mStatusText = (TextView) findViewById(R.id.tv_msg);
mStatusText.append("正在等待用户连接主机\n");
mBlueToothName = (TextView) findViewById(R.id.tv_bluetooth_name);
mBlueToothName.setText(mBluetoothAdapter.getName());
mBlueAddress = (TextView) findViewById(R.id.tv_bluetooth_address);
mBlueAddress.setText(mBluetoothAdapter.getAddress());
mRecyclerView = (RecyclerView) findViewById(R.id.rv_client);
mRecyclerAdapter = new ServerAdapter(this, this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerAdapter);
mHandler = new MyBluetoothHandler(this);
mBluetoothConnection = new MyBluetoothConnection();
bindService(new Intent(this, BluetoothService.class), mBluetoothConnection, BIND_AUTO_CREATE);
}
信息的读取
public BluetoothMessage(@BluetoothMessageUtility.MESSAGE_TYPE int messageType, @NonNull String macAddress){
checkMessageType(messageType);
checkAddressLength(macAddress);
mMessageType = messageType;
mMacAddress = macAddress;
}
主机部分显示的链接相关代码
private static class MyBluetoothHandler extends BluetoothServiceHandler {
private final WeakReference mActivity;
public MyBluetoothHandler(ChatroomActivity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void serverSetupFinished() {
Log.w(TAG, "Should not have received a call to serverSetupFinished");
}
@Override
public void connectionClosed(String macAddress, @BluetoothServiceUtility.CLOSE_CODE int closeCode) {
Toast.makeText(mActivity.get(), BluetoothServiceUtility.getCloseCodeInfo(closeCode) +
" : 与 " + macAddress +" 断开连接", Toast.LENGTH_SHORT).show();
//TODO if server, tell all clients that somebody disconnected.
}
@Override
public void appMessage(String address, byte[] data) {
String msg = new String(data);
if (data.length < BluetoothChatroomUtility.ID_LENGTH) {
Log.w(TAG, "unreadable message " + msg);
return;
}
String messageId = (msg.substring(0, BluetoothChatroomUtility.ID_LENGTH));
String messageData = "";
if (msg.length() > BluetoothChatroomUtility.ID_LENGTH)
messageData = msg.substring(BluetoothChatroomUtility.ID_LENGTH, msg.length());
switch (messageId) {
case BluetoothChatroomUtility.ID_SEND_DISPLAY_TEXT:
mActivity.get().showMessage(messageData);
if (mActivity.get().mIsServer) {
String appMessage = BluetoothChatroomUtility.makeDisplayTextMessage(messageData);
mActivity.get().mBinder.writeMessage(appMessage.getBytes());
}
break;
default:
Log.v(TAG, " unknown chatroom message id " + messageId + ", with message " + messageData);
break;
}
}
}
ServiceConnection mBluetoothConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "BluetoothConnection connected");
mConnected = true;
mBinder = (BluetoothService.BluetoothBinder) service;
mBinder.setHandler(mBT_Handler);
if (mIsServer) {
mBinder.serverReady();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "BluetoothConnection disconnected");
mConnected = false;
mBinder = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mIsServer = getIntent().getBooleanExtra(EXTRA_SERVER, false);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
mName = adapter.getName();
mAddress = adapter.getAddress();
mEditText = (EditText) findViewById(R.id.et_msg);
btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = mEditText.getText().toString();
String message = mName + "-----" + text;
mEditText.setText("");
if (mConnected) {
String chatMessage = BluetoothChatroomUtility.makeDisplayTextMessage(message);
mBinder.writeMessage(chatMessage.getBytes());
}
if (mIsServer)
showMessage(message);
}
});