Android调用蓝牙功能

Android调用蓝牙功能

  • 相关权限
  • 具体使用
    • 相关方法

相关权限


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

具体使用

相关方法

 adapter.startDiscovery(); //开始搜索周围蓝牙设备
 adapter.disable();  //关闭本机蓝牙设备
 adapter.enable(); //是否可用
 
 开启蓝牙: 
 Intent intent = new Intent();
 intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);
 startActivityForResult(intent,100);
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private BluetoothManager manager;
    private BluetoothAdapter adapter;

    private MyBroadcastReceiver myBroadcastReceiver;
    ArrayList<BluetoothDevice> list;
    ListView listView;
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
    }

    public void initView(){

        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
        findViewById(R.id.btn_search).setOnClickListener(this);


        list=new ArrayList<>();
        myAdapter=new MyAdapter(list,this);
        listView=findViewById(R.id.listview);
        listView.setAdapter(myAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    Method createBond = BluetoothDevice.class.getMethod("createBond");
                    createBond.invoke(list.get(position));

                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void initData(){

        manager= (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        adapter=manager.getAdapter();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        myBroadcastReceiver = new MyBroadcastReceiver();
        registerReceiver(myBroadcastReceiver,intentFilter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                boolean enable = adapter.enable();
                if(enable==false){
                    Intent intent = new Intent();
                    intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);
                    startActivityForResult(intent,100);

                }

                break;
            case R.id.btn_search:
                adapter.startDiscovery();
                break;
            case R.id.btn_close:
                adapter.disable();
                list.clear();
                myAdapter.notifyDataSetChanged();

                break;

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100&&resultCode==RESULT_OK){
            Toast.makeText(this, "开启成功", Toast.LENGTH_SHORT).show();

        }

    }

    class MyBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            switch (action){
                case BluetoothDevice.ACTION_FOUND:
                    BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    list.add(device);
                    myAdapter.notifyDataSetChanged();
                    break;
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:

                    break;

                case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                    BluetoothDevice device1=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int bondState = device1.getBondState();

                    if(bondState==BluetoothDevice.BOND_NONE){
                        Toast.makeText(context, "未找到设备", Toast.LENGTH_SHORT).show();
                    }else if(bondState==BluetoothDevice.BOND_BONDING){
                        Toast.makeText(context, "匹配中", Toast.LENGTH_SHORT).show();
                    }else if(bondState==BluetoothDevice.BOND_BONDED){
                        Toast.makeText(context, "配对成功", Toast.LENGTH_SHORT).show();
                    }

                    break;
            }

        }
    }

}

效果:
Android调用蓝牙功能_第1张图片

Android调用蓝牙功能_第2张图片

Android调用蓝牙功能_第3张图片

你可能感兴趣的:(Android)