资源简介
蓝牙连接部分的核心代码,解决Android手机和单片机连接的一些问题。
代码片段和文件信息
package com.ostd.eoiot.bluetooth;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Set;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
public class BlueToothClient extends Thread {
private boolean isConnecting = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice mmDevice = null;
private Method M = null;
private BluetoothSocket btSocket = null;
private InputStream instream = null;
private static OutputStream outstream = null;
static BufferedReader mBufferedReaderClient = null;
static PrintWriter mPrintWriterClient = null;
private static String address ;
private static Handler serviceHandler; // 用于向客户端Service回传消息的handler
public BlueToothClient(Handler handler) {
this.serviceHandler = handler;
}
@SuppressLint(“NewApi“)
public void run()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)//判断设备是否有蓝牙
{
return;
}
if(!mBluetoothAdapter.isEnabled())//判断蓝牙是否打开
{
// 蓝牙设备没打开
serviceHandler.obtainMessage(BluetoothTools.Bluetooth_Closed)
.sendToTarget();
}
// mBluetoothAdapter.startDiscovery();
//
Set devices
= mBluetoothAdapter.getBondedDevices();
if(devices.size()>0)
{
for(Iterator iterator = devices.iterator(); iterator.hasNext();)
{
BluetoothDevice bbDevice = (BluetoothDevice)iterator.next();
address = bbDevice.getAddress();
}
}
try {
mmDevice = mBluetoothAdapter.getRemoteDevice(address);
//btSocket = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
M = mmDevice.getClass().getMethod(“createRfcommSocket“ new Class[]{int.class});
btSocket = (BluetoothSocket)M.invoke(mmDevice 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mBluetoothAdapter.cancelDiscovery();
try
{
btSocket.connect();
instream = btSocket.getInputStream();
outstream = btSocket.getOutputStream();
// 发送连接成功消息,消息的obj参数为连接的
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5642 2014-05-21 09:12 BlueToothClient.java
----------- --------- ---------- ----- ----
5642 1
- 上一篇:java个人简历
- 下一篇:java SM4加密demo
相关资源
- BluetoothChat 蓝牙聊天 Android 官方源码
- 手机控制树莓派软件及代码
- Bluetooth LE for iOS and Android v2.3.unitypac
- Unity插件Bluetooth LE For iOS and Android
- Android 蓝牙4.0 ble 官方绿色源码 demo
- Bluetooth LE for iOS tvOS and Android
- bluecove2.1.2
- 蓝牙串口调试助手源码android Bluetoot
- android蓝牙传输文件
- Bluetooth.apk
- Android中蓝牙的基本使用----BluetoothAd
- 实现android手机蓝牙连接demo
- Bluetooth_4.3 BLE蓝牙通信
- bluetooth 蓝牙demo (ble)
- Android Bluetooth 蓝牙
- android 蓝牙传输BluetoothGet
- android-BluetoothChat 蓝牙2.0版的通信
评论
共有 条评论