这两天在调研一个蓝牙通信的问题,第一次接触,经过多方查找资料,拜访过N多大神博客,终于成功的实现了蓝牙的搜索,配对和连接,但是后续通信问题仍在摸索中。希望此文能够给大家一些帮助!(至于参考的大神博客就不列出了,忘记在哪了(*^__^*) )
效果图:
1、获得蓝牙适配器
就是这个东东:BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
然后判断设备是否支持蓝牙,现在的手持设备应该都支持吧,蓝牙4.0都在普及了。
如果支持蓝牙,看蓝牙是否打开,没有则打开。
代码如下:
<span style="white-space:pre"> </span>/**
* 检查设备是否支持蓝牙,若支持则打开
*/
private void checkBluetooth() {
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
// 设备不支持蓝牙
Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
}else {
// 判断蓝牙是否打开,如果没有则打开蓝牙
// adapter.enable() 直接打开蓝牙,但是不会弹出提示,以下方式会提示用户是否打开
if (!adapter.isEnabled()) {
Intent intent = new Intent();
//打开蓝牙设备
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//是设备能够被搜索
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// 设置蓝牙可见性,最多300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(intent);
}
}
}
2、获取本机已经配对的蓝牙设备
关键就是这句了:Set<BluetoothDevice> devices = adapter.getBondedDevices();
可以放到list集合中,以便在ListView中显示。
bondedDevicesList.addAll(devices);
代码如下:
<span style="white-space:pre"> </span>/**
* 获取所有已经绑定的蓝牙设备
*/
private void getBondedDevices() {
bondedDevicesList.clear();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
bondedDevicesList.addAll(devices);
//为listview动态设置高度(有多少条目就显示多少条目)
setListViewHeight(bondedDevicesList.size());
mBondedAdapter.notifyDataSetChanged();
}
3、注册用以接收到已搜索到的蓝牙设备的receiver
注册receiver
<span style="white-space:pre"> </span>// 注册用以接收到已搜索到的蓝牙设备的receiver
IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
mFilter.addAction(BluetoothDevice.ACTION_FOUND);
mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
mFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
mFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
mFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注册广播接收器,接收并处理搜索结果
registerReceiver(receiver, mFilter);
receiver代码如下:
<span style="white-space:pre"> </span>private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 获得已经搜索到的蓝牙设备
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 搜索到的不是已经绑定的蓝牙设备
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
// 防止重复添加
if (searchDevicesList.indexOf(device) == -1)
searchDevicesList.add(device);
//devicesList.add("未配对 | "+device.getName() + "(" + device.getAddress()+")");
mSearchAdapter.notifyDataSetChanged();
}
// 搜索完成
} else if (action
.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
setProgressBarIndeterminateVisibility(false);
setTitle("搜索完成");
} else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
// 状态改变的广播
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
if (device.getName().equalsIgnoreCase(name)) {
int connectState = device.getBondState();
switch (connectState) {
case BluetoothDevice.BOND_NONE: //10
Toast.makeText(MainActivity.this, "取消配对:"+device.getName(), Toast.LENGTH_SHORT).show();
break;
case BluetoothDevice.BOND_BONDING: //11
Toast.makeText(MainActivity.this, "正在配对:"+device.getName(), Toast.LENGTH_SHORT).show();
break;
case BluetoothDevice.BOND_BONDED: //12
Toast.makeText(MainActivity.this, "完成配对:"+device.getName(), Toast.LENGTH_SHORT).show();
getBondedDevices();
try {
// 连接
connect(device);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
}
};
4、蓝牙设备的连接
连接后就可以进行数据传输了,但是数据传输仍在摸索中。
<span style="white-space:pre"> </span>//蓝牙设备的连接(客户端)
private void connect(BluetoothDevice device) {
// 固定的UUID
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
try {
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
// OutputStream outputStream = socket.getOutputStream();
// InputStream inputStream = socket.getInputStream();
// outputStream.write("StartOnNet\n".getBytes());
// outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
关于后续蓝牙通信的东东,等我研究好了再更新,大家别嫌我写的简单,这是给和我一样刚入门的人学习的。
环境:Android4.4,windows xp,eclipse,UTF-8
源码下载地址:http://download.youkuaiyun.com/detail/shuyou612/8837907