蓝牙

一.使用

用蓝牙时,要先获取四个权限

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

然后动态获取权限

requestPermissions(new String[]{
        Manifest.permission.BLUETOOTH,
        Manifest.permission.BLUETOOTH_ADMIN,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION
}, 101);

使用蓝牙时,要先获取蓝牙管理者,通过蓝牙管理者获取蓝牙适配器(也就是本机蓝牙)

private void initBlueTooth() {
    bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
}

1.开启蓝牙

通过intent对象隐式跳转到蓝牙

private void openBlueTooth() {
    if (!bluetoothAdapter.enable()){//如果蓝牙不可用
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);//开启蓝牙
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//允许蓝牙被搜索
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);//设置允许被搜索时间200s内可以被搜索到
        startActivity(intent);
    }
}

2.关闭蓝牙

直接通过bluetoothAdapter调用disable方法关闭

private void closeBlueTooth() {
    if (bluetoothAdapter != null) {
        bluetoothAdapter.disable();//关闭蓝牙
    }
}

3.获取已配对的设备列表

这里使用RecyclerView作数据展示

<LinearLayout
    android:layout_marginTop="20dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:textSize="30sp"
        android:textStyle="bold"
        android:text="已配对设备"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_pd"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

适配器

public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {

    private List<BluetoothDevice> datas;
    private Context context;

    public Myadapter(List<BluetoothDevice> datas, Context context) {
        this.datas = datas;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private LayoutInflater layoutInflater;


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.item, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.teShow.setText(datas.get(i).getName());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            teShow = itemView.findViewById(R.id.te_show);
        }

        private TextView teShow;

    }
}

然后是获取已配对的设备

Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
pd_content.clear();
pd_content.addAll(bondedDevices);

myadapter.notifyDataSetChanged();

只需要通过本机蓝牙调用getBondedDevices即可

RecyclerView记得要加展示方式

myadapter = new Myadapter(pd_content,this);
rvPd.setAdapter(myadapter);
rvPd.setLayoutManager(new LinearLayoutManager(this));//设置展示方式

4.获取附近的设备

需要有一个广播接收者获取扫描到的广播

class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_NAME);
            sm_content.add(device);
            myadapter_sm.notifyDataSetChanged();
        }
    }
}

扫描到的设备展示,接口回调获取设备创建连接

//扫描
myadapter_sm = new Myadapter(sm_content, this);
rvSm.setAdapter(myadapter_pd);
rvSm.setLayoutManager(new LinearLayoutManager(this));//设置展示方式
myadapter_sm.setMyItemOnClickListener(new MyItemOnClickListener() {
    @Override
    public void OnItemClick(int position) {
        BluetoothDevice device = sm_content.get(position);
        device.createBond();//配对
    }
});

要开心加油

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值