andriod TV 获取已连接蓝牙遥控器电池电量总结

因项目需求的需要,需要实现android TV 机顶盒连接成功的蓝牙遥控器的低电量提醒。根据需求,要先获得已连接的蓝牙设备的电量,再根据电量的值做判断提醒用户。

下面直接看代码,本项目中一共有三个类,初始MainActivity,MyBroadcastReceiver,MyService.其中,MainActivity不做任何修改,MyBroadcastReceiver监听开机完成广播,启动服务MyService,读取电量以及提醒的功能全都在服务中实现。

MyBroadcastReceiver类:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("aaa" , "Boot Completeed .");
        Intent service = new Intent(context,MyService.class);
        context.startService(service);
    }
}

MyService类:

public class MyService extends Service {

    private static final boolean DEBUG = false;
    private static final String TAG = "aaa";
    public static Context context;
    BluetoothDevice mDevice;
    BluetoothGatt mBluetoothGatt;
    IntentFilter filter ;
    private static final UUID GATT_BATTERY_SERVICE_UUID =
            UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb");
    private static final UUID GATT_BATTERY_LEVEL_CHARACTERISTIC_UUID =
            UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");
    private static final String KEY_BATTERY = "battery";
    private Handler handler;

    @Override
    public void onCreate() {
        Log.d("aaa" , "service created.");
        handler = new Handler();
        context = getApplicationContext();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int start
好的,下面是一个简单的蓝牙遥控器应用程序的开发步骤: 1. 首先,你需要在 Android Studio 中创建一个新的项目,并添加蓝牙权限到 AndroidManifest.xml 文件中: ``` <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 2. 在 MainActivity 中,你需要添加一个按钮,并在按钮的点击事件中打开蓝牙: ``` Button btnEnableBluetooth = findViewById(R.id.btnEnableBluetooth); btnEnableBluetooth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device doesn't support Bluetooth } else if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } }); ``` 3. 接下来,你需要添加一个搜索设备的按钮,并在按钮的点击事件中启动一个 Intent,打开设备搜索页面: ``` Button btnSearchDevices = findViewById(R.id.btnSearchDevices); btnSearchDevices.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DeviceListActivity.class); startActivityForResult(intent, REQUEST_CONNECT_DEVICE); } }); ``` 4. 在 DeviceListActivity 中,你需要列出所有可用的蓝牙设备,并在用户点击列表项时返回设备地址: ``` private void setupDevices() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); for (BluetoothDevice device : pairedDevices) { mDeviceArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String deviceInfo = (String) parent.getItemAtPosition(position); String deviceAddress = deviceInfo.substring(deviceInfo.length() - 17); Intent intent = new Intent(); intent.putExtra(EXTRA_DEVICE_ADDRESS, deviceAddress); setResult(Activity.RESULT_OK, intent); finish(); } }); } ``` 5. 在 MainActivity 中,你需要连接到设备,并发送数据: ``` private void connectToDevice(String address) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); try { mBluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID); mBluetoothSocket.connect(); mOutputStream = mBluetoothSocket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } } private void sendData(String data) { try { mOutputStream.write(data.getBytes()); } catch (IOException e) { e.printStackTrace(); } } ``` 这就是一个简单的蓝牙遥控器应用程序的开发过程。当然,这只是一个基础框架,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值