Android 蓝牙的经常使用操做

最近对Android设备的蓝牙操做进行了一些研究, 下面作一些总结, 版本是4.4,列出的解决方案多来源于网络,感谢强大的网友们:html

操做蓝牙能够分为常规的操做,和很是规的操做。所谓常规的操做,就是界面上有提示,须要客户许可进行的一些操做。很是规的则一般是采用反射等手段,达到不知不觉链接蓝牙的目的。android

 

一. 常规操做:网络

1. 获取蓝牙的操做接口:socket

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

蓝牙的相关操做基本都是经过上面这个类。ide

 

2. 打开本机的蓝牙设备:this

if (!mBtAdapter.isEnabled()) {
  Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  startActivityForResult(enableIntent, 0);
}

 

3. 打开蓝牙的可见性:spa

 
 
if (mBtAdapter.isEnabled()) {   Intent visibleIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);   visibleIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  // 后面的ms最多就是300   startActivity(visibleIntent); }

 

4. 开启了蓝牙设备,就是为了与其余设备通讯,因此须要扫描周围可用的设备:.net

// 注册两个intent,并定义receiver接受蓝牙设备检测过程当中,"发现设备"和"完成"的回调事件
IntentFilter discoveryFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
IntentFilter discoveryFinishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
BroadcastReceiver discoverReceiver = 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);  // 拿到别的device才能进行链接操做
     } 
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
{
     // DO Something
}
}
};
this.registerReceiver(discoverReceiver, discoveryFilter);
this.registerReceiver(discoverReceiver, discoveryFinishedFilter);

/************************************/

mBtAdapter.startDiscovery(); // 用来开始搜索周围可见的蓝牙设备 // 若是在发现过程当中想要中止,能够调用下面的API if(mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); return; }

 

 5. 做为server端,等待其余设备链接:线程

BluetoothServerSocket mServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
BluetoothSocket mClientSocket = mServerSocket.accept();

// 有了socket对象,能够获取stream了, 能够用stream的read,write方法来读写数据了,read能够在独立线程的循环里,以保证持续接受到数据。
InputStream inStream = mClientSocket.getInputStream();
OutputStream outStream = mClientSocket.getOutputStream();

 

6. 做为client端,能够去链接server:code

BluetoothDevice mDevice = mBtAdapter.getRemoteDevice(device.getAddress());   //首先要得到server的设备对象,这个兑现能够在以前 discover的时候就拿到,也能够经过以前记录的address获取到

// connect 若是成功了,就获得socket链接了,以后和上面同样,就能够经过stream进而收发消息了
mClientSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mClientSocket.connect();

 

 

二. 很是规操做:

 

1. 打开本机的蓝牙设备:

// 这样是不会弹出对话框的
if
(!mBtAdapter.isEnabled()) {   mBtAdapter.enable(); }

 

2. 打开蓝牙的可见性:


  //值得一提的是,这个方法在 mBtAdapter.enable(); 以后当即调用是颇有可能不生效的。多是蓝牙设备开启须要一些时间。因此,最好是在确认设备确实已经开启了以后(好比说sleep一下子,或者有个有个循环不断check mBtAdapter.isEnabled  (),来保证设备已经准备好了。  
  public
void setDiscoverableTimeout(int timeout) {

     try {
          Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
            setDiscoverableTimeout.setAccessible(true);
            Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
            setScanMode.setAccessible(true);
             
            setDiscoverableTimeout.invoke(mBtAdapter, timeout);
            setScanMode.invoke(mBtAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

3. 不配对就进行蓝牙通讯:

// 经过这两个API, 能够很神奇的, 不配对蓝牙设备就进行通讯
mServerSocket = mBtAdapter.listenUsingInsecureRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mClientSocket = mDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));

 

三. 最后列一下须要的权限:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 

蓝牙参考网页: 

http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android

http://stackoverflow.com/questions/5885438/bluetooth-pairing-without-user-confirmation

http://blog.csdn.net/zshq280017423/article/details/7645622

http://blog.csdn.net/menghnhhuan/article/details/7057484

http://blog.csdn.net/eric41050808/article/details/16967189

http://www.2cto.com/kf/201312/261093.html

WIFI 参考网页:

http://blog.csdn.net/ranger1111/article/details/6777153

http://lszdb1983.blog.163.com/blog/static/20426348201209251344/

 

格式啊,怎么也搞很差,,,

相关文章
相关标签/搜索