关于android4.3 bluetooth4.0的那些事儿

   马年伊始,刚刚上班的一个星期,公司里没什么事儿可做,只是听说马上可能要做蓝牙的项目。之前也做过关于软硬件通讯之类的项目:android 串口通讯,android usb 转串口通讯。

可是蓝牙这块还真是没有怎么接触过,只是以前每天论坛看博客看到过,跟网络通讯差不多,也是用到socket诸如此类的关键字,做通讯函数。可是这蓝牙4.0我听说是新的蓝牙协议,由TI公司做出来开发板的。没接触过,伤,真心伤!!!没办法,苦逼的程序员不都是这样的么!

  蓝牙4.0为蓝牙3.0的升级标准,拥有极低的运行和待机功耗。Bluetooth Low Energy。针对android系统开发关于蓝牙4.0apk,首先要确定你所用的蓝牙开发板,用的最多的就是TI个公司的cc2540。android系统方面,如果是用android 的sdk那么系统就要在4.3以上了,如果是三星的sdk或者moto的sdk就没有那么的要求了,只要根据他提供的sdk说明来开发就行了。没办法老板死活不给三星的研发设备,所以本吊丝只能用android自带的sdk开发了,所以本篇随笔也只是针对Android4.3和ble的了。

  官方android4.3 BLE开发文档:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

  官方android4.3 BLE开发sample:http://developer.android.com/samples/BluetoothLeGatt/index.html

  pc端调试工具:

我相信有了官方文档的话开发起来应该不是很困难了,起码有了找到组织的赶脚。

废话太多了:

  想要你的app能使用蓝牙,那么permissions是必然的,没有权限是干不了事情的:

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

  那么想要你的设备只有ble的有效那么还需要下面这句话:

1 <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

  还要看你的设备是不是支持BluetoothLE设备。

    // 判断此设备是否支持蓝牙4.0设备
        if (!getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT)
                    .show();
            finish();
        }

  如果你的设备支持蓝牙4.0那么下面要做的事情就是初始化一下关于要用的蓝牙的参数了:

 1 // Initializes Bluetooth adapter.
 2         final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 3         mBluetoothAdapter = bluetoothManager.getAdapter();
 4 
 5         // Is adapter null?
 6         if (mBluetoothAdapter == null) {
 7             Toast.makeText(this, "bluetooth4.0 is not supported!",
 8                     Toast.LENGTH_SHORT).show();
 9             this.finish();
10             return;
11         }

  关于上面第三行代码BluetoothAdapter mBluetoothAdapter。这个adapter可是个非常有用的东西,可以这么理解,它就是你手机的蓝牙。

  看来你已经成功拿到你操控的蓝牙了,那么就要看看它当前是否能用。也就是要打开蓝牙。我是用ToggleButton这个控件来控制。

 1 tb_on_off.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 2 
 3             @Override
 4             public void onCheckedChanged(CompoundButton buttonView,
 5                     boolean isChecked) {
 6                 if (isChecked) {
 7                     Intent enableBtIntent = new Intent(
 8                             BluetoothAdapter.ACTION_REQUEST_ENABLE);
 9                     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
10                 } else {
11                     mBluetoothAdapter.disable();
12                 }
13             }
14         });

  打开蓝牙后,下面就要搜索外围设备了。

 1 /**
 2      * @Title: scanLeDevice
 3      * @Description: 搜索设备
 4      * @param @param enable
 5      * @return void
 6      * @throws
 7      */
 8     private void scanLeDevice(final boolean enable) {
 9         if (enable) {
10             mHandler.postDelayed(new Runnable() {
11 
12                 @Override
13                 public void run() {
14                     mScanning = false;
15                     mBluetoothAdapter.stopLeScan(mLeScanCallback);
16                 }
17             }, SCAN_PERIOD);
18 
19             mScanning = true;
20             mBluetoothAdapter.startLeScan(mLeScanCallback);
21         } else {
22             mScanning = false;
23             mBluetoothAdapter.stopLeScan(mLeScanCallback);
24         }
25     }
private BluetoothAdapter.LeScanCallback mLeScanCallback = new LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi,
                byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDevListAdapter.addDevice(device);
                    mDevListAdapter.notifyDataSetChanged();
                }
            });
        }
    };

搜索到你想要的设备后连接设备。

BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

BluetoothGatt我所理解的就是一台计算机的cup,收发处理数据都要靠它。

设备已经连接上了那么就可以了干一些事情了,比如收发数据。这里就不详细说明了,我会把demo上传来,仅供参考。

对了还要补充一点,就是ble,android4.3sdk中数据传输不是用socket这样的关键字了。这个官方文档已经说的很详细了,所以这篇博客写的比较粗糙。^-^!!!

demo连接:http://pan.baidu.com/s/1bn061nL

原文地址:https://www.cnblogs.com/dividxiaoshuo-fighting/p/3559891.html