【TL8266】顯示BLE設備列表

資料來源:網峯的APP源碼
http://www.smartwebee.com/products_detail/productId=27.html

這裏寫圖片描述
這裏寫圖片描述
推薦跟我一起操作,在主頁面定義了一個按鈕用來刷新,一個用來停止,一個進度條用來提示,一個list用來顯示設備列表,這個可以把原來的內容全部刪掉,直接複製
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stop_button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scan_button"/>
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressbar"/>
    <ListView
        android:id="@+id/id_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

第一個代碼量比較大的部分就是做列表的適配器,對這部分比較瞭解的可以快速跳看,核心是圍繞着集合mDeviceList在做,瀏覽時可以Ctrl鍵+F,查看代碼對它的操作


public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private class ViewHolder{
        private TextView deviceName;
        private TextView deviceAddress;
    }
    private class ListViewAdapter extends BaseAdapter{

        private ArrayList<BluetoothDevice> mDeviceList;
        private LayoutInflater mLayoutInflater;

        private ListViewAdapter(){
            mDeviceList=new ArrayList<>();
            mLayoutInflater=MainActivity.this.getLayoutInflater();
        }

        private void addDevice(BluetoothDevice device){
            if (!mDeviceList.contains(device))mDeviceList.add(device);
        }

        private void clear(){
            mDeviceList.clear();
        }

        @Override
        public int getCount() {
            return mDeviceList.size();
        }

        @Override
        public Object getItem(int i) {
            return mDeviceList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view==null){
                viewHolder=new ViewHolder();
                view=mLayoutInflater.inflate(R.layout.list_view_item, null);

                viewHolder.deviceAddress=view.findViewById(R.id.address_text);
                viewHolder.deviceName=view.findViewById(R.id.name_text);
                view.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) view.getTag();
            }
            BluetoothDevice device=mDeviceList.get(i);//注意這裏
            String deviceNameString=device.getName();
            if (deviceNameString!=null&&deviceNameString.length()>0){
                viewHolder.deviceName.setText(deviceNameString);
            }else {
                viewHolder.deviceName.setText("其他藍牙設備");
            }
            viewHolder.deviceAddress.setText(device.getAddress());

            return view;
        }
    }

}//Activity

相關的listitem
list_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name_text"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/address_text"/>

</LinearLayout>

做好了適配器以後,我們進行主體編寫
onCreate中都用init函數寫,方便調試!

public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

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

        initView();
        initData();
        initBLE();
        initEvent();
    }

}

四個函數分別是初始化頁面(button),初始化數據(button,handler,adapter),初始化BLE(檢查設備是否有藍牙,請求打開藍牙),初始化事件(註冊按鈕點擊,設置adapter,掃描函數),可直接複製,記得註冊點擊事件時在MainActivity 後面implements View.OnClickListener

public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private Handler handler;
    private boolean mScanning;

    private Button mScanButton;
    private Button mStopButton;
    private ProgressBar mProgressBar;

    private BluetoothAdapter mBluetoothAdapter;
    private ListViewAdapter mListViewAdapter;
    private ListView mListView;

    private void initView(){

        mScanButton=findViewById(R.id.scan_button);
        mStopButton=findViewById(R.id.stop_button);
        mProgressBar=findViewById(R.id.progressbar);
        mListView=findViewById(R.id.id_list_view);

    }

    private void initData(){
        mScanButton.setText("掃描");
        mStopButton.setText("停止");
        mListViewAdapter=new ListViewAdapter();
        handler=new Handler();

    }

    private void initBLE(){
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "不支持BLE", Toast.LENGTH_SHORT).show();
            finish();
        }
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "不支持藍牙", Toast.LENGTH_SHORT).show();
            finish();
        }

        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
            if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){

                if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION))
                    Toast.makeText(this,"需要定位權限", Toast.LENGTH_LONG).show();

                ActivityCompat.requestPermissions(this ,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);

            }
        }

        if (!mBluetoothAdapter.isEnabled()) {
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 130);
            }
        }
    }

    private void initEvent(){

        mStopButton.setOnClickListener(this);
        mScanButton.setOnClickListener(this);
        mListView.setAdapter(mListViewAdapter);
        mListView.setOnItemClickListener(this);
        Scan(true);

    }

}

藍牙請求結果回調,列表點擊事件,按鍵點擊事件

public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // User chose not to enable Bluetooth.
        if (requestCode == 130 && resultCode == Activity.RESULT_CANCELED) {
            finish();
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.scan_button:
                mListViewAdapter.clear();
                Scan(true);
                break;
            case R.id.stop_button:
                Scan(false);
                break;

        }
    }

    private void setButtonVisible(boolean mScanning){
        if (!mScanning){
            mStopButton.setVisibility(View.GONE);
            mScanButton.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.GONE);
        }else {
            mStopButton.setVisibility(View.VISIBLE);
            mScanButton.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        BluetoothDevice device= (BluetoothDevice) mListViewAdapter.getItem(i);
        if (device!=null)
        Toast.makeText(this,device.getAddress(),Toast.LENGTH_SHORT).show();
    }

}

現在來到整篇文章的核心部分,就是掃描方法和回調,兩個方法也可以直接複製

BluetoothAdapter.LeScanCallback是藍牙掃描的回調,核心在於BluetoothDevice的操作,可以按Ctrl鍵+F看bluetoothDevice的去向
Scan方法用於啓動和停止掃描,initEvent中調用了一次,傳入true表示進行一次掃描,傳入false停止掃描,在按鈕的onClick方法中也調用了


public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private BluetoothAdapter.LeScanCallback mBluetoothAdapterLeScanCallback=new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mListViewAdapter.addDevice(bluetoothDevice);
                    mListViewAdapter.notifyDataSetChanged();
                }
            });
        }
    };
    private void Scan(boolean enable){
        if (enable){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mBluetoothAdapterLeScanCallback);//會提示需要藍牙操作權限
                    setButtonVisible(mScanning);
                }
            },10000);
            mScanning=true;
            mBluetoothAdapter.startLeScan(mBluetoothAdapterLeScanCallback);

        }else {
            mScanning=false;
            mBluetoothAdapter.stopLeScan(mBluetoothAdapterLeScanCallback);
        }
        setButtonVisible(mScanning);
    }

}

清單文件,這裏幾個權限要注意,一般這裏出錯比較多
除了藍牙權限外,如果需要BLE feature則還需要聲明uses-feature
required爲true時,則應用只能在支持BLE的Android設備上安裝運行;required爲false時,Android設備均可正常安裝運行,需要在代碼運行時判斷設備是否支持BLE feature,就是前面initBLE()中的FEATURE_BLUETOOTH_LE判斷

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.a123">

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

源碼地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章