android 开发 蓝牙开发

首先分享2个写的很好的蓝牙博客,非常全面,只是个别细节没有照顾到。

https://blog.csdn.net/a1533588867/article/details/52442349

https://blog.csdn.net/s13383754499/article/details/78436023

首先添加蓝牙权限一组:

 <!-- 蓝牙权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

废话不多说,直接上代码,都有注释:

1.打开蓝牙,关闭蓝牙。

package com.example.lenovo.mydemoapp.btDemo;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.lenovo.mydemoapp.R;

public class BTDemo extends AppCompatActivity implements View.OnClickListener{
    private final String TAG = "BTDemo";
    private Button btn_bt_open,btn_bt_close,btn_bt_goSttings,btn_bt_visible,in_BTList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_btdemo);
        btn_bt_open = (Button)findViewById(R.id.btn_BT_open);
        btn_bt_close = (Button)findViewById(R.id.btn_BT_close);
        btn_bt_visible = (Button)findViewById(R.id.btn_BT_visible);
        btn_bt_goSttings = (Button)findViewById(R.id.btn_BT_GoSttings);
        in_BTList = (Button)findViewById(R.id.in_BTList);
        btn_bt_open.setOnClickListener(this);
        btn_bt_close.setOnClickListener(this);
        btn_bt_visible.setOnClickListener(this);
        btn_bt_goSttings.setOnClickListener(this);
        in_BTList.setOnClickListener(this);
    }
    //打开蓝牙
    public void openBT(){
        //创建蓝牙适配器
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null){
            Log.e(TAG, "该设备不支持蓝牙");
        }
        if(!bluetoothAdapter.isEnabled()){
            Log.e(TAG, "准备打开蓝牙" );
            //弹窗询问方式打开蓝牙
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);//蓝牙适配器. 行动-请求-打开
            //startActivity(intent);也可以使用这个
            startActivityForResult(intent,RESULT_OK);
            //bluetoothAdapter.enable(); 不询问直接打开蓝牙
        }
    }
    //关闭蓝牙
    public void  closeBT(){
        Log.e(TAG, "coloseBT 被调用" );
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter.isEnabled()){
            bluetoothAdapter.disable();
        }
    }
    //跳转到设置-蓝牙界面中
    public void settingsOpen(){
        Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
        startActivity(intent);
    }
    //蓝牙可见(蓝牙可以被其他设备发现)
    public void btVisible(){
        BluetoothAdapter blueteoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //开启被其它蓝牙设备发现的功能
        //getScanMode 获得扫描模式                                    扫描-模式-连接-发现
        if (blueteoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//行动-请求-发现
            //设置为一直开启 0是一直开着,如果设置了时间就会按照设置时间显示蓝牙可见
            i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
            startActivity(i);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_BT_open:
                openBT();
                break;
            case R.id.btn_BT_close:
                closeBT();
                break;
            case R.id.btn_BT_visible:
                btVisible();
                break;
            case R.id.btn_BT_GoSttings:
                settingsOpen();
                break;
            case R.id.in_BTList:
                Intent intent = new Intent(BTDemo.this,BTListView.class);
                startActivity(intent);
                break;
            default:
                break;
        }

    }
}


2.蓝牙搜索与蓝牙内容列表显示:


package com.example.lenovo.mydemoapp.btDemo;

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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lenovo.mydemoapp.R;

import java.util.List;

public class BTListView extends AppCompatActivity {
    private ListView listView;
    private ArrayAdapter mArrayAdapter;
    private BluetoothAdapter mBluetoothAdapter;
    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_btlist_view);
        //创建蓝牙适配器
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //搜索蓝牙
        mBluetoothAdapter.startDiscovery();

        listView = (ListView) findViewById(R.id.BTlistView);
        //创建listView的适配器
        mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);
        //意图过滤器
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        //创建广播接收器
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //获得intent的行动
                String action = intent.getAction();
                /*
                三组蓝牙广播状态分别是:
                BluetoothAdapter.ACTION_DISCOVERY_STARTED  开始蓝牙搜索
                BluetoothDevice.ACTION_FOUND  蓝牙搜索中
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED 蓝牙搜索完毕
                 */
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //创建蓝牙设备,我们可以从BluetoothDevice 里获得各种信息 名称、地址 等等
                    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // 将设备名称和地址放入array adapter,以便在ListView中显示
                    mArrayAdapter.add(bluetoothDevice.getName() + "\n" + bluetoothDevice.getAddress());
                } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                    Toast.makeText(BTListView.this,"开始搜索", Toast.LENGTH_SHORT).show();
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    Toast.makeText(BTListView.this,"搜索完毕",Toast.LENGTH_SHORT).show();
                }
            }
        };
        //添加广播寄存器
        registerReceiver(broadcastReceiver,intentFilter);
        listView.setAdapter(mArrayAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBluetoothAdapter.cancelDiscovery();// 取消搜索蓝牙
        unregisterReceiver(broadcastReceiver);//注销广播接收器

    }
}

猜你喜欢

转载自blog.csdn.net/qq_37217804/article/details/80743149