要使用BLE服務須先開啟藍芽權限

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

定義 BleManagement 物件負責處理BLE相關工作.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class BleManagement {
    private Context context;

    private Handler mHandler;
    private BluetoothManager bluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private static final long SCAN_PERIOD = 10000; //10 seconds

    private BluetoothAdapter.LeScanCallback leScanCallback;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public BleManagement(Context context){
        this.context = context;
        this.mHandler = new Handler();

        initble();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public BleManagement(Context context, BluetoothAdapter.LeScanCallback LeScanCallback){
        this.context = context;
        this.mHandler = new Handler();
        this.leScanCallback =  LeScanCallback;

        initble();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void initble(){
        bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
    }

    public boolean IsDeviceSupportBLE(){
        return context.getPackageManager().hasSystemFeature (PackageManager.FEATURE_BLUETOOTH_LE);
    }

    public boolean IsOpenBluetooth(){
       return (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void scanLeDevice(final boolean enable) {
        if (enable) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {mBluetoothAdapter.stopLeScan(leScanCallback);
                }
            }, SCAN_PERIOD);

            mBluetoothAdapter.startLeScan(leScanCallback);
        } else {
            mBluetoothAdapter.stopLeScan(leScanCallback);
        }
    }

    public BluetoothAdapter.LeScanCallback getLeScanCallback() {
        return leScanCallback;
    }

    public void setLeScanCallback(BluetoothAdapter.LeScanCallback leScanCallback) {
        this.leScanCallback = leScanCallback;
    }
}

 

建立BleManagement物件並利用其方法確認目前硬體裝置的支援成度.

若未開啟藍芽,則透過Intent請求使用者開啟藍芽服務.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void bleInit(){
    bleManagement = new BleManagement(this,mLeScanCallback);

    if(!bleManagement.IsDeviceSupportBLE()){
        Toast.makeText(this, "硬體不支援", Toast.LENGTH_SHORT).show();
        finish();
    }

    if(!bleManagement.IsOpenBluetooth()){
        Toast.makeText(this, "請開啟藍芽裝置", Toast.LENGTH_SHORT).show();
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
    } else {
        bleManagement.scanLeDevice(true);
    }
}

 

 

建立LeScanCallback,當掃描到藍芽裝置時會呼叫該函數.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi,
                                 final byte[] scanRecord) {
                Log.d("TAG", "BLE device : " + device.getName());

                try{
                    Beacon beacon = new Beacon(scanRecord
                                                 ,device,rssi);

                    String message = "ibeaconName" +
                            "\nMac:" + beacon.getMacAddress()
                            + " \nUUID:" + beacon.getUuid()
                            + "\nMajor:" + beacon.getMajor()
                            + "\nMinor:" + beacon.getMinor()
                            + "\nTxPower:" + beacon.getTxPower()
                            + "\nrssi:" + rssi;

                    Log.d(Tag, message);

                    Log.d(Tag, "distance:" + beacon.distance());

                    Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                }catch (BeaconFormateNotFoundException e){
                    Toast.makeText(context,"Beacon Formate Error",Toast.LENGTH_SHORT).show();
                }
            }
        };

 

 

arrow
arrow
    文章標籤
    Android ble
    全站熱搜

    Lung-Yu,Tsai 發表在 痞客邦 留言(1) 人氣()