最近公司在搞IBeacon,因为公司另一个学android的走了,而剩下的人中,只有我接触过java、android,所以只有我来做这个了。

声明,我是一个C#的程序员,java、android都是弱项,只是略有涉及,不懂、错误之处,多多指教。

一开始,我在网上搜IBeacon的资料(使用百度,唉,看来我还是2B程序员啊),最详细的就只有两个,而这两个都是同一个人的,hellogv,播客地址:http://my.csdn.net/hellogv

我找到的两个博文,

http://blog.csdn.net/hellogv/article/details/24267685

http://blog.csdn.net/hellogv/article/details/24661777

因为一开是没有接触过这个东西,所以看不懂啊。只好把代码下载下来,去实际运行一下。

当然,我先下载的是搜索基站的那个,不然没有基站,我上哪里去通信的???!!!

对了,这个BLE是在android 4.3.1版本及以上才能使用哦。

好了,废话少说。

(1)打开蓝牙

这步不多说什么了。

获取BluetoothManager、BluetoothAdapter

 1     /** 2      * Initializes a reference to the local Bluetooth adapter. 3      *  4      * @return Return true if the initialization is successful. 5      */ 6     public boolean initialize() 7     { 8         // Use this check to determine whether BLE is supported on the device.  Then you can 9         // selectively disable BLE-related features.        10         if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))11         {12             Log.e(TAG, "Unable to initialize Bluetooth.");13             return false;14         }15         // For API level 18 and above, get a reference to BluetoothAdapter through16         // BluetoothManager.17         if (mBluetoothManager == null)18         {19             mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);20             if (mBluetoothManager == null)21             {22                 Log.e(TAG, "Unable to initialize BluetoothManager.");23                 return false;24             }25         }26         27         mBluetoothAdapter = mBluetoothManager.getAdapter();28         if (mBluetoothAdapter == null)29         {30             Log.e(TAG, "Unable to obtain a BluetoothAdapter.");31             return false;32         }33         34         return true;35     }36     

然后打开蓝牙

    public boolean OpenBlue()    {        //开启蓝牙        if (!mBluetoothAdapter.isEnabled())            return mBluetoothAdapter.enable();        else            return true;    }

因为我写C#习惯了,vs代码样式就是这样的,所以,嘿嘿,能看就行啦哈

(2)扫描

 1     private void scanLeDevice(final boolean enable) 2     { 3         if (enable)//enable =true就是说要开始扫描 4         { 5             // Stops scanning after a pre-defined scan period. 6             // 下边的代码是为了在 SCAN_PERIOD 后,停止扫描的 7             // 如果需要不停的扫描,可以注释掉 8             mHandler.postDelayed(new Runnable() 9             {10                 @Override11                 public void run()12                 {13                     mScanning = false;14                     mBluetoothAdapter.stopLeScan(mLeScanCallback);15                     // 这个是重置menu,将 menu中的停止按钮->扫描按钮16                     invalidateOptionsMenu();17                 }18             }, SCAN_PERIOD);19             20             mScanning = true;//此变量指示扫描是否进行21             mBluetoothAdapter.startLeScan(mLeScanCallback);//这句就是开始扫描了22         }23         else24         {25             mScanning = false;26             mBluetoothAdapter.stopLeScan(mLeScanCallback);//这句就是停止扫描27         }28         // 这个是重置menu,将 menu中的扫描按钮->停止按钮29         invalidateOptionsMenu();30     }

这里有个变量 mLeScanCallback,这个是什么呢?

 1     // Device scan callback. 2     private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() 3     { 4         @Override 5         public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) 6         { 7             iBeacon iBeacon = fromScanData(device, rssi, scanRecord);20         }21     };22     

通过探索呢,这个东西就是一个回调函数,当蓝牙接收到某广播时,就会调用 mLeScanCallback.onLeScan()函数。device当然就是广播的设备了,rssi是信号强度,scanRecord是广播的信息。

然后通过解析scanRecord就可以知道设备的详情了。formScanData我就不多说了,在大神的源码里有。

获取到IBeacon以后,你就可以把他放到列表里,该怎么显示就是你的问题了。

更多相关文章

  1. android实现蓝牙耳机的连接及列表的管理
  2. android手机通过串口蓝牙透传模块与AVR单片机通信实例。。。蓝牙
  3. Android中图片实现按钮点击效果
  4. android Shape Drawable创建两边半圆的按钮背景
  5. Android 获取无线蓝牙MAC信息代码
  6. Android 三个按钮对话框
  7. Android布局学习之――按钮居中
  8. android 多款按钮样式
  9. android-制作透明按钮

随机推荐

  1. Android检查设备是否联网
  2. Android(安卓)Wear创建通知的几种方式
  3. android 程序全屏设置
  4. android使用keystore打包错误解决方法
  5. hello android
  6. Android(安卓)flutter Json转Dart Model
  7. android中的sqlite操作
  8. android识别鼠标左键,右键操作
  9. Android(安卓)界面设计工具 droiddraw
  10. Android基于TitleBar页面导航实现