단거리 통신
6937 단어 활용단어참조
관련 절차
서버 측의 대기 응답 및 링크 피드백
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("
");
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);
}
});
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
제이티의 사용에 대한 상세한 설명Continuation 메커니즘을 이용하여 대량의 사용자 요청과 비교적 긴 연결을 처리한다.또한 Jetty는 매우 좋은 인터페이스를 설계했기 때문에 Jetty의 어떤 실현이 사용자의 수요를 만족시키지 못할 때 사용자...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.