浅谈本人对Android中BlueTooth的学习理解:
首先贴出效果图:
![](https://img-blog.csdn.net/20170321170822800?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzQ5NDI2ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
然后是在使用蓝牙过程中需要的权限详情:
在清单文件中配置权限,权限一共有两个需要注意的地方:蓝牙权限和定位权限:蓝牙权限:<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
定位权限:<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
这个地方为什么会用到定位权限呢?因为在蓝牙搜索附近设备的时候是需要定位的,
如果没有在没有定位权限的场景下在本程序中直接配对连接蓝牙设备是会出现配对不成功的问题的,有时甚至搜索附近的蓝牙设备
都搜索不到。
以下是在利用蓝牙通讯过程中用到的代码,这是初始化蓝牙以及实现通讯功能的主页面的activity
public class ChatActivity extends AppCompatActivity {
private Button mBtnSend;//发送消息的按钮
private EditText mEditTextContent;//输入框
private TextView tx;//是否连接的TextView
private ChatMsgViewAdapter mAdapter;//聊天Listview的适配器
private ListView mListView;
private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();//存储数据的集合
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;//点击按钮1 建立新的蓝牙连接
private static final int REQUEST_ENABLE_BT = 3;//打开系统的蓝牙
/**
* 连接上的蓝牙设备的名字
*/
private String mConnectedDeviceName = null;
/**
* 聊天线程的适配器
*/
private ArrayAdapter<String> mConversationArrayAdapter;
/**
*传出消息
*/
private StringBuffer mOutStringBuffer;
/**
* 本地的蓝牙适配器
*/
private BluetoothAdapter mBluetoothAdapter = null;
/**
* 聊天服务
*/
private BluetoothChatService mChatService = null;
/**
* 获取信息从bluetoothchatservice处理程序
*/
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
//连接完成
tx.setText(mConnectedDeviceName + " 连接到 " + mBluetoothAdapter.getName());
break;
case BluetoothChatService.STATE_CONNECTING:
tx.setText("连接中。。。");
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
//连接失败
tx.setText("无连接");
break;
}
break;
//接收 发送的消息
case Constants.MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// 从缓冲区中构造一个字符串
String writeMessage = new String(writeBuf);
//mConversationArrayAdapter.add("Me: " + writeMessage);
send(writeMessage);
break;
//接收 收到的消息
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// 在缓冲区中的有效字节中构造一个字符串
String readMessage = new String(readBuf, 0, msg.arg1);
receive(readMessage);
break;
case Constants.MESSAGE_DEVICE_NAME:
// 保存连接设备的名称
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != this) {
Toast.makeText(ChatActivity.this, "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
}
break;
case Constants.MESSAGE_TOAST:
if (null != this) {
Toast.makeText(ChatActivity.this, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
initView();
initData();
//获得BluetoothAdapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断有没有蓝牙设备
if (mBluetoothAdapter == null) {
Log.e("错误", "设备没有蓝牙模块");
finish();
}
}
@Override
protected void onStart() {
super.onStart();
/** 打开蓝牙设备*/
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else if (mChatService == null) {
setupChat();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mChatService != null) {
mChatService.stop();
}
}
@Override
public void onResume() {
super.onResume();
// 在onresume()执行此检查覆盖的情况下,BT是在onstart()没有启用,
// 所以我们停了下来,使它…onresume()时将调用返回action_request_enable活动。
if (mChatService != null) {
// 只有国家state_none,我们知道我们还没有开始
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
// 启动蓝牙聊天服务
mChatService.start();
}
}
}
/**
* 发送按钮监听 发送消息
*/
private void setupChat() {
mBtnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/** 通过按钮点击监听发送消息的相关处理*/
sendMessage();
}
});
// 初始化bluetoothchatservice进行蓝牙连接
mChatService = new BluetoothChatService(this, mHandler);
// 初始化传出消息的缓冲区
mOutStringBuffer = new StringBuffer("");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE_SECURE:
// 当devicelistactivity返回与设备连接
if (resultCode == Activity.RESULT_OK) {
connectDevice(data, true);
}
break;
case REQUEST_ENABLE_BT:
// 当请求启用蓝牙返回时
if (resultCode == Activity.RESULT_OK) {
// 蓝牙现在启用,所以设置一个聊天会话
setupChat();
} else {
// 用户没有启用蓝牙或发生错误
Toast.makeText(this, "蓝牙开启失败!",
Toast.LENGTH_SHORT).show();
finish();
}
}
}
/**
* 连接设备
*/
private void connectDevice(Intent data, boolean secure) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BluetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
mChatService.connect(device, secure);
}
/**
* 让本设备可见
*/
private void ensureDiscoverable() {
if (mBluetoothAdapter.getScanMode() !=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
/**
* 初始化控件
*/
private void initView() {
mListView = (ListView) findViewById(R.id.chat_list_view);
mBtnSend = (Button) findViewById(R.id.btn_send);
mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
tx = (TextView) findViewById(R.id.tx);
}
private final static int COUNT = 8;
//初始化要显示的数据
private void initData() {
mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
mListView.setAdapter(mAdapter);
}
/**
* 点击发送按钮时传送数据的方法
*/
private void sendMessage() {
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(this, "未连接蓝牙设备!", Toast.LENGTH_SHORT).show();
mEditTextContent.setText("");
return;
}
String contString = mEditTextContent.getText().toString();
if (contString.length() > 0) {
//得到消息的字节写入bluetoothchatservice
byte[] send = contString.getBytes();
mChatService.write(send);
// 将字符串缓冲区重置为零,并清除编辑文本字段
mOutStringBuffer.setLength(0);
mEditTextContent.setText(mOutStringBuffer);
} else if (contString.length() == 0) {
Toast.makeText(this, "发送的消息不能为空", Toast.LENGTH_SHORT).show();
}
}
/**
* 通过handler得到发送的消息 建立实体类 添加到集合中
*
* @param msg
*/
private void send(String msg) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate(getDate());
entity.setName("我");
entity.setMsgType(false);
entity.setText(msg);
mDataArrays.add(entity);
mAdapter.notifyDataSetChanged();
mListView.setSelection(mListView.getCount() - 1);
}
/**
* 通过handler得到收到的消息 建立实体类 添加到集合中
*
* @param msg
*/
private void receive(String msg) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate(getDate());
entity.setName(mConnectedDeviceName);
entity.setMsgType(true);
entity.setText(msg);
mDataArrays.add(entity);
mAdapter.notifyDataSetChanged();
mListView.setSelection(mListView.getCount() - 1);
}
/**
* 获取时间
*
* @return
*/
private String getDate() {
Calendar c = Calendar.getInstance();
String year = String.valueOf(c.get(Calendar.YEAR));
String month = String.valueOf(c.get(Calendar.MONTH));
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
String mins = String.valueOf(c.get(Calendar.MINUTE));
StringBuffer sbBuffer = new StringBuffer();
sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins);
return sbBuffer.toString();
}
/**
* 扫描蓝牙 延时搜索
*
* @param view
*/
public void abc(View view) {
switch (view.getId()) {
case R.id.bt1:
Intent serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
break;
case R.id.bt2:
//请求本设备是否可被搜索到
ensureDiscoverable();
break;
}
}
}
以下是展示聊天信息的实体类
public class ChatMsgEntity {
private static final String TAG = ChatMsgEntity.class.getSimpleName();
private String name;//谁被名称
private String date;//当前时间
private String text;//内容
private boolean msgType = true;//类型 true表示发送
public boolean getMsgType() {
return msgType;
}
public void setMsgType(boolean msgType) {
this.msgType = msgType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public ChatMsgEntity() {
}
public ChatMsgEntity(String name, String date, String text, boolean msgType) {
this.name = name;
this.date = date;
this.text = text;
this.msgType = msgType;
}
}
定义一个常量类接口
常量接口
public interface Constants {
// 从bluetoothchatservice句柄发送消息类型
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// 从bluetoothchatservice处理接收的关键的名字
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
}
查询附近设备的activity
public class DeviceListActivity extends Activity {
private static final String TAG = "DeviceListActivity";
/**
* 返回的地址
*/
public static String EXTRA_DEVICE_ADDRESS = "device_address";
/**
*适配器
*/
private BluetoothAdapter mBtAdapter;
/**
* 发现的新的设备
*/
private ArrayAdapter<String> mNewDevicesArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_device_list);
setResult(Activity.RESULT_CANCELED);
Button scanButton = (Button) findViewById(R.id.button_scan);
scanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doDiscovery();
v.setVisibility(View.GONE);
}
});
ArrayAdapter<String> pairedDevicesArrayAdapter =
new ArrayAdapter<String>(this, R.layout.device_name);
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
pairedListView.setAdapter(pairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(mDeviceClickListener);
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
for (BluetoothDevice device : pairedDevices) {
pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else {
String noDevices = "没有设备配对过";
pairedDevicesArrayAdapter.add(noDevices);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBtAdapter != null) {
mBtAdapter.cancelDiscovery();
}
this.unregisterReceiver(mReceiver);
}
/**
* 开始查询设备
*/
private void doDiscovery() {
setProgressBarIndeterminateVisibility(true);
setTitle("扫描设备中");
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
mBtAdapter.startDiscovery();
}
/**
* 点击监听
*/
private AdapterView.OnItemClickListener mDeviceClickListener
= new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it's costly and we're about to connect
mBtAdapter.cancelDiscovery();
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// 设置结果
setResult(Activity.RESULT_OK, intent);
finish();
}
};
/**
* 查询设备完成
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 发现设备
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// 结束
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle("选择要连接的设备");
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = "没有发现设备";
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
}
创建的服务public class BluetoothChatService {
private static final String TAG = "BluetoothChatService";
private static final String NAME_SECURE = "BluetoothChatSecure";
private static final String NAME_INSECURE = "BluetoothChatInsecure";
private static final UUID MY_UUID_SECURE =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
private AcceptThread mSecureAcceptThread;
private AcceptThread mInsecureAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private int mState;
public static final int STATE_NONE = 0;
public static final int STATE_LISTEN = 1;
public static final int STATE_CONNECTING = 2;
public static final int STATE_CONNECTED = 3;
public BluetoothChatService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
private synchronized void setState(int state) {
mState = state;
mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
public synchronized int getState() {
return mState;
}
public synchronized void start() {
Log.d(TAG, "start");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
setState(STATE_LISTEN);
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread(true);
mSecureAcceptThread.start();
}
if (mInsecureAcceptThread == null) {
mInsecureAcceptThread = new AcceptThread(false);
mInsecureAcceptThread.start();
}
}
public synchronized void connect(BluetoothDevice device, boolean secure) {
Log.d(TAG, "connect to: " + device);
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectThread = new ConnectThread(device, secure);
mConnectThread.start();
setState(STATE_CONNECTING);
}
public synchronized void connected(BluetoothSocket socket, BluetoothDevice
device, final String socketType) {
Log.d(TAG, "connected, Socket Type:" + socketType);
// 已经完成连接,所以结束ConnectThread
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
// 结束所有正在运行的ConnectedThread
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// 结束所有正在运行的AcceptThread因为我们只要和一个蓝牙设备通信!
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
if (mInsecureAcceptThread != null) {
mInsecureAcceptThread.cancel();
mInsecureAcceptThread = null;
}
// 开启一个新的ConnectedThread用于管理消息的读写
mConnectedThread = new ConnectedThread(socket, socketType);
mConnectedThread.start();
// 把已经连上的蓝牙设备名发给UI Activity
Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(Constants.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
/**
* Stop 所有线程
*/
public synchronized void stop() {
Log.d(TAG, "stop");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
if (mInsecureAcceptThread != null) {
mInsecureAcceptThread.cancel();
mInsecureAcceptThread = null;
}
setState(STATE_NONE);
}
public void write(byte[] out) {
ConnectedThread r;
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
r.write(out);
}
private void connectionFailed() {
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(Constants.TOAST, "Unable to connect device");
msg.setData(bundle);
mHandler.sendMessage(msg);
BluetoothChatService.this.start();
}
private void connectionLost() {
Message msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(Constants.TOAST, "Device connection was lost");
msg.setData(bundle);
mHandler.sendMessage(msg);
BluetoothChatService.this.start();
}
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
private String mSocketType;
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
try {
if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
MY_UUID_SECURE);
} else {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
NAME_INSECURE, MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
Log.d(TAG, "Socket Type: " + mSocketType +
"BEGIN mAcceptThread" + this);
setName("AcceptThread" + mSocketType);
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothChatService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice(),
mSocketType);
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
}
public void cancel() {
Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
}
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
setName("ConnectThread" + mSocketType);
// 在执行连接时务必关闭蓝牙发现以提高效率
mAdapter.cancelDiscovery();
// 创建一个 BluetoothSocket 连接
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() " + mSocketType +
" socket during connection failure", e2);
}
connectionFailed();
return;
}
// 已经完成蓝牙连接,重置ConnectThread
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// 连接完成,开启监听
connected(mmSocket, mmDevice, mSocketType);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// 当已经连接上蓝牙设备后保持连接
while (true) {
try {
// 读InputStream
bytes = mmInStream.read(buffer);
// 发送读取的消息到UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
BluetoothChatService.this.start();
break;
}
}
}
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
}