zoukankan      html  css  js  c++  java
  • android IBeacon 开发(一)搜索IBeacon基站

    最近公司在搞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 through
    16         // 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                 @Override
    11                 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         else
    24         {
    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以后,你就可以把他放到列表里,该怎么显示就是你的问题了。

  • 相关阅读:
    “error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载)
    std::ostringstream 转std::string
    【转载】红外感应模块+蜂鸣器实现简易报警
    分割字符串使用空格
    优秀程序员的 18 大法则【转载】
    解决win10 iot VS编程出现的无法引用错误
    [转载] 程序员如何成功追到女神?
    win8/win10/win2012r2 存储池 冗余分析
    [转载][NAS] 使用win8的“存储池”功能~
    mySQL中删除unique key的语法 (删除某个字段的唯一性)
  • 原文地址:https://www.cnblogs.com/sdwdjzhy/p/4521421.html
Copyright © 2011-2022 走看看