手机App与蓝牙手柄

在蓝牙手柄与手机连接成功了后 进入APP中,你会发现App里面有很多你想通过蓝牙手柄选择的地方,选择不到、无法获得焦点;

在你的布局里面加入这个android:focusableInTouchMode="true"(任何时候强制获得焦点,就是说不管你是蓝牙手柄还是认为点击)或者android:focusable="true"(只有在蓝牙手柄的情况下会获得焦点,如果蓝牙手柄没有连接的话则不会干扰其他事件)
怎么加:比如你的这个TextView有一个点击事件,在使用蓝牙手柄的时候希望蓝牙手柄能够选中它(textview控件)
<TextView
    android:id="@+id/tx"
    android:onClick="aaa"
    android:focusable="true"
    android:background="@drawable/select_video"//当Textview被虚拟选中的时候出现的样式,改变背景颜色或者出现边框。
android:layout_width="wrap_content" android:layout_height="wrap_content" />

select_video.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/new_hot_text2"></item>
    <item android:state_focused="true" android:drawable="@color/colorPrimaryDark">
    </item>
    <!--<item android:drawable="@color/colorAccent" />-->
</selector>

以下是一个简单的划船机蓝牙从机连接手机app手柄主机的配置例程: ``` #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; // 定义手柄手机app主机的UUID #define HANDSET_UUID "0000fff0-0000-1000-8000-00805f9b34fb" #define APP_UUID "0000fff1-0000-1000-8000-00805f9b34fb" // 定义手柄手机app主机的特征值 uint8_t handsetValue[20] = {0}; uint8_t appValue[20] = {0}; // 划船机蓝牙从机的回调函数 class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { // 连接成功后初始化特征值 pCharacteristic->setValue(handsetValue, 20); pCharacteristic->notify(); } void onDisconnect(BLEServer* pServer) { // 断开连接后清空特征值 memset(handsetValue, 0, 20); } }; // 手柄主机的回调函数 class HandsetCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { // 接收手柄主机的数据并更新特征值 std::string value = pCharacteristic->getValue(); memcpy(handsetValue, value.c_str(), value.length()); } }; // 手机app主机的回调函数 class AppCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { // 接收手机app主机的数据并更新特征值 std::string value = pCharacteristic->getValue(); memcpy(appValue, value.c_str(), value.length()); } }; void setup() { BLEDevice::init("RowingMachine"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // 添加手柄主机的特征值 BLEService* pHandsetService = pServer->createService(BLEUUID(HANDSET_UUID)); pCharacteristic = pHandsetService->createCharacteristic( BLEUUID(HANDSET_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->setCallbacks(new HandsetCallbacks()); // 添加手机app主机的特征值 BLEService* pAppService = pServer->createService(BLEUUID(APP_UUID)); pCharacteristic = pAppService->createCharacteristic( BLEUUID(APP_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->setCallbacks(new AppCallbacks()); // 开始广播 pHandsetService->start(); pAppService->start(); BLEAdvertising* pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void loop() { // 定时更新特征值 pCharacteristic->setValue(handsetValue, 20); pCharacteristic->notify(); delay(10); } ``` 在上述代码中,我们使用了ESP32 BLE库来实现划船机蓝牙从机的功能。首先,在`setup()`函数中初始化BLE设备并创建BLE server,然后添加手柄主机和手机app主机的服务和特征值,并设置回调函数。在`loop()`函数中定时更新特征值。在手柄主机和手机app主机连接到从机时,会触发`onConnect()`回调函数,我们可以在该函数中初始化特征值。当手柄主机或手机app主机发送数据到从机时,会触发相应的回调函数,我们可以在回调函数中接收数据并更新特征值。当手柄主机或手机app主机断开连接时,会触发`onDisconnect()`回调函数,我们可以在该函数中清空特征值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值