移动互联网开发 Android Studio 使用
类微信实现Bluetooth聊天
需求要求
两个连接的手机选择后可进行蓝牙聊天
部分代码展示
要想实现蓝牙聊天,首先需要获得蓝牙权限,在项目的配置文件AndroidManifest.xml加入下面两条代码
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
检查该设备是否支持蓝牙连接
private void isSupportBluetooth() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null
|| !getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
showNotSupportBluetoothDialog();
Log.e(TAG, "not support bluetooth");
} else {
Log.e(TAG, " support bluetooth");
}
}
主动发起连接的线程ConnectThread
private class ConnectThread extends Thread{
private final BluetoothSocket mmSocket;
private fina