2015年4月15日 星期三

Android-Bluetooth Low Energy(BLE) 基本操作( 一 )

相信大家對BLE已經聽到快爛了吧
但還是要說個重點
BT 不等於 BLE
BLE 等於 BT

今天先簡單操作說明 :

1 . SDK 版本 : 18

2 . 加入權限 :
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

3 . 頁面 :
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHandler = new Handler();
        
        bluetoothManager = (BluetoothManager) 
                getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        // 檢查手機硬體是否為BLE裝置
        if (!getPackageManager().hasSystemFeature
                (PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "硬體不支援", Toast.LENGTH_SHORT).show();
            finish();
            
        }

        // 檢查手機是否開啟藍芽裝置
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this, "請開啟藍芽裝置", Toast.LENGTH_SHORT).show();
            Intent enableBluetooth = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
            
        } else {
            scanLeDevice(true);
            
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        scanLeDevice(false);

    }

    // 掃描藍芽裝置
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

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

    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback = 
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi,
                final byte[] scanRecord) {
            // 搜尋回饋
            Log.d("TAG","BLE device : " + device.getName());

        }
    };

}


//則打開藍芽
Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enabler);

//則打開藍芽( 強迫 )
adapter.enable();



歡迎轉載,請註明出處。

4 則留言:

  1. WoSports S6 Bluetooth Sunglasses Save 50% each on Qualifying items offered by WoSports when you purchase 1 or more. Enter code 9QOZSF9M at checkout.
    Here: WoSports S6 Bluetooth Sunglasses

    WoSports Sunglasses Bluetooth Headphones Polarized Glasses Headset with Handsfree Answer Phone Music Mp3 Player for Android IOS Smartphones
    Here: WoSports S6 Bluetooth Sunglasses

    回覆刪除
  2. 版主您好
    我想請問如果要將藍牙掃描的功能改成在背景的service執行的話
    應該要怎麼改寫比較好

    回覆刪除
  3. 版主您好:
    最近剛接觸藍牙相關的手機開發,想要進行一些測試,是否有完整的code可以看? 謝謝

    回覆刪除