相关Android蓝牙知识直接略过,大家可以上百度搜搜。
此代码经过项目验证,完全OK
直接上代码步骤:
private BluetoothAdapter adapter;
1.// 获取蓝牙适配器
adapter = BluetoothAdapter.getDefaultAdapter();
// 打开蓝牙
if (!adapter.isEnabled()) {
// 弹出对话框提示用户选择是否打开蓝牙
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 0);
// 或者不做提示,强行打开
// bAdapter.enable();
}
2. 获取蓝牙MacAddress
// 搜索并获取已经配对蓝牙
nameAddress = getPairedDevice(adapter);//变量自己添加定义
// 获取名字列表和地址列表
if (nameAddress.size() > 0) {
blueNames = nameAddress.get(0);
blueAddress = nameAddress.get(1);
}
// 获取方法 此处我把名字和地址分别存放到集合里面,为了方便在ListView里面显示和其他用途,可以根据自己需要,或只返回地址即可
private ArrayList<ArrayList<String>> getPairedDevice(BluetoothAdapter adapter) {
ArrayList<ArrayList<String>> nameAndAddress = new ArrayList<ArrayList<String>>();
// pairedDeviceNames = new ArrayList<String>();
pairedDeviceAddress = new ArrayList<String>();
Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// 逐个解析
for (BluetoothDevice devices : pairedDevices) {
// 放到集合中
pairedDeviceNames.add(devices.getName() + "\n"
+ devices.getAddress());
pairedDeviceAddress.add(devices.getAddress());
}
// 保存信息
nameAndAddress.add(pairedDeviceNames);
nameAndAddress.add(pairedDeviceAddress);
}
return nameAndAddress;
}
3. 通过Address连接蓝牙设备
// 连接设备
device = adapter.getRemoteDevice(address);
connect(device);
public synchronized void connect(BluetoothDevice device) {
connectThread = new ConnectThread(device, this, adapter, handler,handlerUpdate);
connectThread.start();
}
4. 连接蓝牙线程类
public class ConnectThread extends Thread {
// 此处变量略
public ConnectThread(BluetoothDevice device, BluetoothAdapter mAdapterr) {
this.mAdapter = mAdapter;
int sdk = Build.VERSION.SDK_INT;
if (sdk >= 10) {
try {
mySocket = device
.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
mySocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void run() {
//
mAdapter.cancelDiscovery();
try {
mySocket.connect();
// 启动接收远程设备发送过来的数据
connectBluetooth = new ReceiveDatas(mySocket,receiveHandler);
connectBluetooth.start();
//输出流
mmOutStream = mySocket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
try {
mySocket.close();
} catch (IOException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}
}
}
// 写数据
/* Call this from the main Activity to send data to the remote device */
public void sendMessage(String msg) {
byte[] buffer = new byte[16];
try {
if (mmOutStream == null) {
Log.i("info", "输出流为空");
return;
}
// 写数据
buffer = msg.getBytes();
mmOutStream.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (mmOutStream != null) {
mmOutStream.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5. 读取数据类
public class ReceiveDatas extends Thread {
// 变量 略过
// 构造方法
public ReceiveDatas(BluetoothSocket socket) {
this.mmSocket = socket;
InputStream tempIn = null;
// 获取输入流
try {
tempIn = socket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
}
mmInStream = tempIn;
}
@Override
public void run() {
byte[] buffer = new byte[1024];// 缓冲数据流
int bytes;// 返回读取到的数据
// 监听输入流
while (true) {
try {
bytes = mmInStream.read(buffer);
// 此处处理数据……
} else {
Log.i("info", "异常");
}
} catch (IOException e) {
try {
if (mmInStream != null) {
mmInStream.close();
}
Log.i("info", "异常");
break;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
Thread.sleep(50);// 延迟
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}