Android监听蓝牙状态

https://blog.csdn.net/y2653904/article/details/84890506
https://blog.csdn.net/u014361280/article/details/108150955

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
public class BlueToothLinsenerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                switch (blueState) {
                    case BluetoothAdapter.STATE_TURNING_ON:
                        Log.e("onReceive", "---------蓝牙正在打开中");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Log.e("onReceive", "---------蓝牙已经打开");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        Log.e("onReceive", "---------蓝牙正在关闭中");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        Log.e("onReceive", "---------蓝牙已经关闭");
                        break;
                }
                break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BlueToothLinsenerReceiver btlr
            = new BlueToothLinsenerReceiver();
    this.registerReceiver(btlr, makeFilter());

    System.out.println("isBTConnected == " + isBTConnected());
}

private IntentFilter makeFilter() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    return filter;
}

public static boolean isBTConnected() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    // 获取蓝牙状态
    return bluetoothAdapter.isEnabled();
}

蓝牙列表使用 ScrollView嵌套ListView只显示一个解决
https://blog.csdn.net/u010375364/article/details/51911316

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //测量的大小由一个32位的数字表示,前两位表示测量模式,后30位表示大小,这里需要右移两位才能拿到测量的大小
        int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}
posted @ 2022-09-28 11:55  MikiKawai  阅读(491)  评论(0编辑  收藏  举报