标签: android-bluetooth

处理程序和多个活动

好的,我是android的新手,我正在尝试创建一个通过蓝牙与arduino接口的应用程序。我已经看过示例BluetoothChat,并看过它如何使用Handler在“服务”,由它产生的线程和MainActivity之间进行通信。我的问题是我有多个活动需要使用蓝牙服务。对于每个活动,我都有一个像这样的处理程序:

        mHandler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what){
           case BtService.CHANGE_STATE:
               if (message.arg1 == BtService.STATE_CONNECTING){
                   Intent i = new Intent (MainActivity.this,ConnectedActivity.class);
                   startActivity(i);

               }
               break;
           }
        }

    };
Run Code Online (Sandbox Code Playgroud)

在服务构造函数中,我得到了以下信息:

    private BtService(){
    btm = BluetoothAdapter.getDefaultAdapter();
    mHandler= new Handler(Looper.getMainLooper());
}
Run Code Online (Sandbox Code Playgroud)

当我需要发送消息时,请执行以下操作:

    private synchronized void setState(int state){
    mHandler.obtainMessage(CHANGE_STATE, state, -1).sendToTarget();
    mState = state;
}
Run Code Online (Sandbox Code Playgroud)

但是其他各种处理程序中均未接收到消息。在声明“特定线程的所有Handler对象都接收相同的消息”。所以我不明白这个问题。我是否需要在每次启动活动时将在该活动中声明的处理程序传递给服务以使其接收消息?这似乎可行,但对我而言似乎不是一个好习惯。

multithreading android handler android-bluetooth

2
推荐指数
1
解决办法
3170
查看次数

我如何使用安卓设备通过蓝牙发送文件

在我的应用程序中,我将我的设备与目标配对。之后,我的有界列表中有该设备:

BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) adapter.getBondedDevices().toArray(new BluetoothDevice[0]);

for (BluetoothDevice d : mAllBondedDevices) {
    Log.d("trace", d.getName().toString()); //check it here
}
Run Code Online (Sandbox Code Playgroud)

现在我想将文件发送到我的目标,就像我的手机如何将文件发送到任何移动设备或计算机一样。

我必须使用反射来发送文件吗?或者这个问题有其他一些解决方案?

我的目标设备是电脑和手机

android bluetooth android-bluetooth

2
推荐指数
1
解决办法
3486
查看次数

Android,服务:获取BluetoothManager返回null

希望你能帮助我解决这个问题.

我正在将我的蓝牙代码从一个Activity移动到一个服务,但它无法将BluetoothManager置于服务中的构造函数中.它一直返回null.你知道为什么吗?

public class BluetoothService extends Service {

private Handler handler;
private static int secondsBetweenScans;
private BluetoothAdapter mBluetoothAdapter;

private Handler mHandler;

private boolean mScanning;

private Map<String, Integer> foundBluetoothDevicesAndTheirSignalStrengths;
private ArrayList<String> foundBluetoothDevices;

// Stops scanning after 10 seconds.
private static final long SCAN_DURATION = 2000;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public BluetoothService() {
    Log.i("KontaktBluetoothService", "BluetoothService initialized");

    // Initializes Bluetooth adapter.
    final BluetoothManager mbluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); <-- Returns null
    mBluetoothAdapter = mbluetoothManager.getAdapter();

    .
    .
    .

}

@Override …
Run Code Online (Sandbox Code Playgroud)

android android-service android-bluetooth

2
推荐指数
1
解决办法
4293
查看次数

在GATT Android中获得BLE特性的价值

我正在尝试使用以下代码读取特征并获取其值

private byte[] val;

mBluetoothLeService.readCharacteristic(characteristic);// reads it
val=characteristic.getValue();
String s=new String(val);
Run Code Online (Sandbox Code Playgroud)

我的应用程序崩溃给出错误"应用程序可能在其主线程上做了太多工作."

我也试过了

private String s;
 mBluetoothLeService.readCharacteristic(characteristic);// reads it
    s=characteristic.getStringValue(0);
Run Code Online (Sandbox Code Playgroud)

但错误是一样的

我调试了我的代码以检查它是否获得了值

private String s;
     mBluetoothLeService.readCharacteristic(characteristic);// reads it
        s=characteristic.getStringValue(0);
    system.out.println(s);
Run Code Online (Sandbox Code Playgroud)

它显示正确的值,但在运行代码为Android App时.我犯了同样的错误.

android bluetooth-lowenergy android-bluetooth gatt

2
推荐指数
1
解决办法
4212
查看次数

任何在Android-L预览中实现BLE通知的方法

这个问题不是关于Android notificatinos,而是关于BLE通知(标题可能暗示)

我已经在Android-L上运行了基本的BLE外设模式

有没有办法在Android-L预览中实现BLE通知.我可以做一些类似下面的事情,使charecteritic能够通知,但试图听

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(
        UUID.fromString(serviceOneCharUuid),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );
Run Code Online (Sandbox Code Playgroud)

但在iOS上的LightBlue应用程序中,我无法订阅此特性.显然,在订阅char时没有可用于响应调用的API(就像在iOS中一样)

如果您已成功在Android-L上启用BLE通知,请分享您的代码

bluetooth-lowenergy android-bluetooth android-5.0-lollipop

2
推荐指数
1
解决办法
4976
查看次数

检测附近的所有蓝牙设备(耳机,电话等),而不会强制设备处于可发现模式

我的目标:

从我的Android应用程序中检测所有附近的蓝牙设备(电话,耳机等).

这是developer.android.com的一个很好的例子,它发现附近的蓝牙设备以及已经配对的设备列表.

我的情况:

我打开了两个蓝牙耳机,成功的蓝牙扫描后没有检测到它们!所以我深入研究了这个问题,并且在某处发现蓝牙耳机需要切换到配对模式以防被android检测到.

要在配对模式下切换耳机,我必须长按电源按钮,同时打开它们.是的,最后,现在我的应用程序扫描检测到蓝牙耳机.

我的问题:

我希望我的耳机会自动检测,而不需要在配对模式下切换它们.找不到检测所有打开的蓝牙设备的方法.

android bluetooth bluetooth-lowenergy android-bluetooth

2
推荐指数
1
解决办法
2万
查看次数

蓝牙Le Gatt没有找到任何设备

答案:特别感谢下面的Nelson Hoang指导我正确的方向并提供有价值的资源.一旦添加了添加位置权限的代码(请参阅下面的Edit2),我必须手动进入设置并切换应用程序的位置权限.一旦完成,它就能很好地工作.

问:我正在尝试使用android studio中的标准蓝牙LE Gatt连接蓝牙LE设备.我每次都被迫从2.8到2.10更新gradle.我已经在不同的项目中自动和手动完成了这项工作.

但是,一旦我将这个应用程序下载到我的Galaxy S7上,它就会扫描并找不到任何内容,尽管它们旁边有3个蓝牙LE设备.我知道这不是我的S7的硬件问题因为我使用了Bluefruit LE并且能够接收信号并且连接得很好.有解决方案吗

编辑:根据建议,我将在下面发布扫描代码.然而,这些都不是我实际写的.这是您可以从Android Studio访问的蓝牙LE示例代码.

Edit2:为了授予应用程序locatin权限,我添加到清单中:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)

这是通常的蓝牙套件的补充,但它没有改变任何东西.

package com.example.android.bluetoothlegatt;

import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Activity for scanning and displaying available Bluetooth LE devices.
 */
public class DeviceScanActivity extends ListActivity {
    private LeDeviceListAdapter …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-bluetooth

2
推荐指数
1
解决办法
3703
查看次数

getBluetoothLeAdvertiser()返回null

BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
Run Code Online (Sandbox Code Playgroud)

返回null.我已尝试使用API​​ 21和API 23设备,但结果相同.我不知道我错过了什么?

该应用程序构建和运行正常,直到当然使用广告客户和应用程序崩溃.

我感谢您提供的任何帮助!:)

android bluetooth-lowenergy android-bluetooth

2
推荐指数
1
解决办法
2259
查看次数

删除 Android 上所有配对的蓝牙设备

我想以编程方式删除 Android 手机上名称以“ABC”开头的配对蓝牙低功耗设备。

我正在使用 Android 工作室。

android bluetooth bluetooth-lowenergy android-bluetooth

2
推荐指数
1
解决办法
5691
查看次数

低功耗蓝牙配对与未配对通信

据我了解,蓝牙低功耗通信可以在配对或不配对的情况下建立。这是在移动开发的背景下,更具体地说是 Android,但我相信 iOS 或多或少是相同的。

是否有一种情况会选择一种而不是另一种?目的是什么?从技术上讲,什么是配对通信,什么是非配对通信?

我已经涉足了一些差异,甚至制作了一些与 BLE 相关的仅演示应用程序,但我还没有找到明确的解释,如果我正在做的事情实际上被认为是配对的。

编辑:

我问这个问题的原因是我相信我希望加密未配对的 BLE 连接。在某些情况下,基本上是我的主要用例,移动设备可能希望在一天中的不同时间随机连接到几个不同的外围设备,而物理上接受配对请求的过程似乎是不必要的,而且非常耗时。“随机”我的意思是,如果我的公寓周围散落着十几个,我会从一个身边走过,而我个人不知道具体是哪一个,而无需亲自检查。我不知道第一次走进房间时必须手动配对每个设备,如果我有 100 个设备,那将是疯狂的。请注意,这些设备不一定必须同时连接,但可以。另请注意,我了解这不是

bluetooth bluetooth-lowenergy android-bluetooth ios-bluetooth

2
推荐指数
1
解决办法
1127
查看次数