android 藍牙操作

android 藍牙操作的demo


demo:下載地址


打開權限:

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


源碼:

package com.bobo.study.study_3_1;
//在Mainfest中註冊相關權限
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Iterator;
import java.util.Set;

public class MainActivity extends Activity implements View.OnClickListener{
    Button but1,but2,but3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but1 = (Button) findViewById(R.id.but1);
        but1.setOnClickListener(this);
        but2 = (Button) findViewById(R.id.but2);
        but2.setOnClickListener(this);
        but3 = (Button) findViewById(R.id.but3);
        but3.setOnClickListener(this);

        IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BluetoothReceiver receiver=new BluetoothReceiver();
        registerReceiver(receiver, filter);
    }

    private class BluetoothReceiver extends BroadcastReceiver {
        @Override
        //接受系統的廣播
        public void onReceive(Context context, Intent intent) {
            //過濾出系統的關於找到的藍牙設備的廣播
            BluetoothDevice blueDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(MainActivity.this,blueDevice.getAddress(),Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        //BluetoothAdapter代表本地的藍牙適配器
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (v == but1) {
            //設置本機藍牙在500S內可被其它藍牙檢測到
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
            //調用系統界面提示用戶選擇
            startActivity(intent);
        } else if (v == but2) {
            //掃描周圍的藍牙設備
            adapter.startDiscovery();//每掃描到一個藍牙設備,系統會自動發送一個廣播
        }else if(v==but3){
            if (adapter != null) {//判斷手機是否有藍牙設備
                if (!adapter.isEnabled()) {//判斷當前藍牙設備是否可用
                    //跳出一個界面,提示用戶是否打開藍牙(該界面由系統提供)
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivity(intent);
                }
                //BluetoothDevice代表遠程的藍牙適配器(即你要連接的目標的藍牙)
                Set<BluetoothDevice> devices = adapter.getBondedDevices();
                if (devices.size() > 0) {
                    for (Iterator iterator = devices.iterator(); iterator.hasNext();) {
                        BluetoothDevice device = (BluetoothDevice) iterator.next();
                        //顯示遠程藍牙設備的地址
                        Toast.makeText(this, device.getAddress(), Toast.LENGTH_SHORT).show();
                    }
                }
            } else {
                Toast.makeText(this, "本機沒有藍牙設備", Toast.LENGTH_LONG).show();
            }
        }
    }
}


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