- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了一个应用程序,它使用特定的蓝牙模块(HC-06)与其通信,但首先它需要与之配对,它使用以下代码来实现……
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ListView lView;
ArrayList<String> devNameList, devAddressList;
private ListAdapter arrayAdapter;
private ConnectThread mConnectThread;
private BluetoothSocket mmSocket = null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private final static int REQUEST_ID = 2;
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String devName = device.getName();
String devAddress = device.getAddress();
devAddressList.add(devAddress);
if (devName == null){
devName = device.getAddress();
}
devNameList.add(devName);
lView.setAdapter(arrayAdapter);
}
}
};
private class ConnectThread extends Thread {
ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "ERROR: Socket's create() method failed", Toast.LENGTH_SHORT).show();
}
mmSocket = tmp;
}
public void run() {
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mmSocket.close();
} catch (IOException closeException) {
Toast.makeText(getBaseContext(), "ERROR: Could not close the client socket",
Toast.LENGTH_SHORT).show();
}
return;
}
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
manageMyConnectedSocket();
}
// Closes the client socket and causes the thread to finish.
void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "ERROR: Could not close the client socket",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_device);
requestPermissions();
lView = findViewById(R.id.discList);
devNameList = new ArrayList<>();
devAddressList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, devNameList);
if (mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String devName = devNameList.get(position);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if((Objects.equals(devName, "HC-06"))|| (Objects.equals(devName, "00:21:13:00:97:6A"))){
Toast.makeText(AddNewDevice.this, "Please wait...", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.cancelDiscovery();
String address = devAddressList.get(position);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mConnectThread = new ConnectThread(device);
mConnectThread.run();
}
else{
Toast.makeText(AddNewDevice.this, "Please connect to an HC-06 device.", Toast.LENGTH_SHORT).show();
}
}
else {
if("HC-06".equals(devName) || "00:21:13:00:97:6A".equals(devName)){
Toast.makeText(AddNewDevice.this, "Please wait...", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.cancelDiscovery();
String address = devAddressList.get(position);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mConnectThread = new ConnectThread(device);
mConnectThread.run();
}
else{
Toast.makeText(AddNewDevice.this, "Please connect to an HC-06 device.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void requestPermissions(){
int androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= 23){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
}, REQUEST_ID);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
mBluetoothAdapter.cancelDiscovery();
}
private void manageMyConnectedSocket() {
mConnectThread.cancel();
finish();
}
该代码在运行 API 17 (Android 4.2.2) 的设备上运行良好,但在 API 23 (android 6.0) 设备上它有点故障:
首先,它有时会找到设备名称,有时却找不到(这就是为什么我在第 50 行和第 51 行 [如果您复制了代码]、第 23 行和24 没有导入 header )
其次,我注意到当它没有获得设备名称并且我尝试连接时,它很难配对/连接到设备(在我的蓝牙设置中它永远显示“正在配对...”)但是当它确实获得了正确配对的设备名称。
谁能帮我解决这个问题?
最佳答案
几天前我回答了这个问题。链接如下。您在使用较新版本的 Android 时遇到的问题是,它需要粗略和精细位置权限才能使用较新版本的 android 进行发现。包含这些权限后,查找设备然后与它们配对应该就可以了。
链接:Can not discover available Bluetooth devices
来自上面链接的我的帖子:
尝试同时添加粗略定位权限。我的应用程序有 Fine 和 Coarse,它适用于 API 19 (Kit Kat)、API 21 (Lollipop)、API 23 (Marshmallow) 和 API 24 (Nougat)。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
另外,请确保您在到达应用程序的这一点之前请求了许可。因此在 onCreate 中添加以下方法以确保在更高版本的 Android 上请求您的权限。我知道我还需要添加此代码才能使其正常工作。您可以为 REQUEST_ID 使用您自己定义的 int。当您为更高版本的 android 加载应用程序时,它会弹出提示给用户。
private int androidVersion; //define at top of code as a variable
private void requestPermissions(){
androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= 23){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
}, REQUEST_ID);
}
}
关于安卓工作室 : Discovered Bluetooth device name not found and struggling to pair to device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47779875/
我使用线程将数据发送到我的 USB 设备(不是数据存储)并从设备获取返回数据。 它在前 5 分钟内运行良好。数据输入,数据输出。 (我几乎在第六分钟开始唱歌。) 然后突然失去了联系。 有logcat消
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: adb cannot find my device for Android debugging. Why? 我是安卓
我有一台安装了win10的电脑, 我已在外部硬盘上安装了 ubuntu 16.04, 现在,当我启动计算机并且外部硬盘已连接到电脑时 grub 菜单打开,我可以在 win10 或 ubuntu 之间进
假设我想使用 Microsoft.Azure.Devices 包/SDK 加载设备信息,这与我所说的有什么区别: RegistryManager regManager = RegistryManage
我在 Windows PC 上使用 VS-2017。我可以使用模拟器测试和运行 iOS 应用程序。 现在我开始为 AppStore 构建应用程序。 构建成功。 现在我在项目解决方案菜单中选择“部署..
任何帮助,将不胜感激。我一般对tensorflow和编程都是陌生的。我正在按照github(https://github.com/experiencor/keras-yolo3)中的说明学习YOLO-
我用 adb 将 4 台设备连接到我的笔记本电脑。 最近我开始看到我的一个 android 设备“离线”,而其他设备运行文件。 我的问题是,离线到底是什么意思,是什么原因造成的? 最佳答案 每次adb
我想在我的物理设备上开始用espresso编写的仪器测试。 想法如下:我的公司有一个应用程序的白标解决方案,因此我们有很多应用程序可以放入 Play 商店。为了支持我们的支持团队,我编写了一个测试,为
当出现这种情况时,我的应用程序当前正在接收与之前的应用程序用户帐户关联的推送通知: 玩家安装应用程序并注册为“bob” bob 的设备 token 在网络服务中注册 玩家重新安装了应用程序并注册为“p
我正在使用命令行编译和安装 Android 应用程序,当我尝试将其安装到设备上时,会发生以下情况: $> adb devices List of devices attached 8c092420
我使用标准 CUDA malloc 在 CUDA 中分配了一个内存数组,并将其传递给函数,如下所示: void MyClass::run(uchar4 * input_data) 我还有一个类成员,它
我的真实设备是 Sony Xperia c6502 Android 版本 4.3我确定我已将它连接到我的计算机并打开了开发人员选项/USB 调试 在 SDK 管理器中,已经安装了 Google USB
某些设备的设备密度返回 0。以下代码用于计算设备密度: WindowManager wm = (WindowManager) context .getSystemService(Con
我正在将一个小文件 (8.5 Mb) 上传到 flask 测试服务器。 当文件上传完成后,服务器报告: File "/home/ubuntu/.virtualenvs/eco_app/lib/
我正在编写一个小应用程序,我需要将一个变量传递给一个函数。问题是该变量是一个绑定(bind),但该函数必须接受一个常规变量。 代码: ForEach($deviceArrays.devices, id
我正在编写一个小应用程序,我需要将一个变量传递给一个函数。问题是该变量是一个绑定(bind),但该函数必须接受一个常规变量。 代码: ForEach($deviceArrays.devices, id
我创建了一个新的 quasar 项目,添加了 cordova 并进行了一些尝试。 一切正常,我可以在手机上部署该应用程序并在模拟器中运行它。 现在,我需要获取一些设备信息,我想为此使用 cordova
根据 W3C The ‘device-width’ media feature describes the width of the rendering surface of the output d
我的数据迭代器当前在 CPU 上运行,因为 device=0 参数已弃用。但我需要它与模型的其余部分一起在 GPU 上运行。 这是我的代码: pad_idx = TGT.vocab.stoi[""]
Cordova 版本:3.5.0 我正在尝试使用 pushPlugin 实现推送通知.但是当我在android平台上运行这个应用程序时(我没有尝试过其他平台),有一个错误“device is not
我是一名优秀的程序员,十分优秀!