安卓蓝牙开发

首先在程序里我们要开启蓝牙权限
在应用程序manifest文件中添加如下代码,声明蓝牙权限。

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

ListView的适配器用于展示蓝牙名字和地址

public class LeDeviceListAdapter extends BaseAdapter {

    private ArrayList<BluetoothDevice> mLeDevices;

    private Activity mActivity;

    private LayoutInflater mInflater;

    public LeDeviceListAdapter(Activity mActivity) {
        this.mActivity = mActivity;
        mInflater  = mActivity.getLayoutInflater();
        mLeDevices = new ArrayList<BluetoothDevice>();
    }

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

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

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

    public void addDevice(BluetoothDevice device){

        if(!mLeDevices.contains(device)){
            mLeDevices.add(device);
        }
    }

    public void clear(){
        mLeDevices.clear();
    }

    private BluetoothDevice getDevice(int position){
        return mLeDevices.get(position);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder;
        if(view == null){
            view = mInflater.inflate(R.layout.device_list, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceName = (TextView)view.findViewById(R.id.device_name);
            viewHolder.deviceAddress = (TextView)view.findViewById(R.id.device_address);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }
         BluetoothDevice device = mLeDevices.get(i);
        viewHolder.deviceName.setText(device.getName());
        viewHolder.deviceAddress.setText(device.getAddress());
        return view;
    }

    class ViewHolder{
        TextView deviceName;
        TextView deviceAddress;
    }
}

蓝牙初始化配置

public class BluetoothLess {


    private static final String TAG = "BluetoothLess";

    private Context mContext;

    public BluetoothAdapter getmBluetoothAdapter() {
        return mBluetoothAdapter;
    }

    public void setmBluetoothAdapter(BluetoothAdapter mBluetoothAdapter) {
        this.mBluetoothAdapter = mBluetoothAdapter;
    }

    public BluetoothLess(Context mContext) {
        this.mContext = mContext;
    }

    private BluetoothAdapter mBluetoothAdapter;

    private BluetoothManager mBluetoothManager;

    public boolean initialied(){
        if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
            if (mBluetoothManager == null) {
                Log.i(TAG, "Unable to initialize BluetoothManager");
                return false;
            }
        }
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Log.i(TAG, "Unable to obtain a BluetoothAdapter.");
            return false;
        }

        // 直接启动蓝牙
        boolean isEnableBTA = mBluetoothAdapter.enable();
        if (isEnableBTA) {
            Log.i(TAG, "mBluetoothAdapter.enable");
        } else {
            Log.i(TAG, "mBluetoothAdapter.disable");
        }

        return isEnableBTA;
    }
}

布局文件

<?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">

    <Button
        android:id="@+id/start_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始扫描"/>

    <Button
        android:id="@+id/stop_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止扫描"/>

    <ListView
        android:id="@+id/device_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button start_btn;
    private Button stop_btn;
    private ListView device_list;

    private LeDeviceListAdapter adapter;

    private BluetoothLess mBluetoothLess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBinds();
        initDatas();
        mBluetoothLess.initialied();
    }

    private void initDatas() {
        mBluetoothLess = new BluetoothLess(this);
        adapter = new LeDeviceListAdapter(this);
        device_list.setAdapter(adapter);
    }

    private void initBinds() {
        start_btn = (Button)findViewById(R.id.start_btn);
        stop_btn = (Button)findViewById(R.id.stop_btn);
        device_list = (ListView)findViewById(R.id.device_list);
        start_btn.setOnClickListener(this);
        stop_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_btn:
                mBluetoothLess.getmBluetoothAdapter().startLeScan(mLeScanCallback);



                break;

            case R.id.stop_btn:
                mBluetoothLess.getmBluetoothAdapter().startLeScan(mLeScanCallback);
                break;
        }

    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.addDevice(bluetoothDevice);
                    adapter.notifyDataSetChanged();
                }
            });
        }
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值