我已经和Android设备连接到BLE设备。如果我重新启动android设备,它将立即连接并正常运行。之后,如果我关闭该应用程序并重新启动,它将连接但从未收到任何特征更改通知。
当我关闭应用程序并重新启动时,它通常可以正常工作。
而且,如果我转到Bluetooth设置并关闭Bluetooth并将其重新打开,则在这种情况下,应用程序也可以直接连接并且可以正常工作。
我正确关闭并清理了所有相关资源,然后正确退出,并且在DDMS中也看不到该应用程序。
但是,即使在驱动程序级别,它似乎仍然连接并且BLE设备仍在发送数据。
谢谢
我正在开发一个带有蓝牙聊天功能的 android 应用程序。我已经成功地用两部手机实现了蓝牙聊天。但我的问题是,如果我从聊天活动更改为下一个活动,连接就会丢失,那么我无法从第二个活动发送消息。如何保持连接?
那就是我想通过我的应用程序保持联系。每当用户按下退出按钮时,只有连接才能断开连接。我想从一个活动发送消息并从另一个活动接收这就是我想要的。我无法使用我的代码创建后台服务。
谁能帮我拆分我的代码?如果我从一部手机收到一条消息,那么我想处理该消息并发回结果,该处理将在下一个活动中进行,这是我的应用程序的工作。
public class BluetoothTexting extends Activity {
private static int DISCOVERY_REQUEST = 1;
private Handler handler = new Handler();
private ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>();
private ArrayAdapter<BluetoothDevice> aa;
private ListView list;
private BluetoothAdapter bluetooth;
private BluetoothSocket socket;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
configureBluetooth();
setupListView();
setupSearchButton();
setupListenButton();
}
private void configureBluetooth() {
bluetooth = BluetoothAdapter.getDefaultAdapter();
}
private void setupListenButton() {
Button listenButton = (Button)findViewById(R.id.button_listen);
listenButton.setOnClickListener(new OnClickListener() {
public void …
Run Code Online (Sandbox Code Playgroud) 我已经通过 HFP 配置文件在蓝牙耳机和 Android 手机之间建立了连接。
现在我想向 HeadSet 设备发送 AT 命令。
我在 HFP 连接时使用了以下组件。
蓝牙耳机.aidl
ACL 的广播接收器
我没有使用任何createRfcommSocketToServiceRecord
套接字连接。
以下功能有什么作用?
boolean sendVendorSpecificResultCode(in BluetoothDevice device,
in String command,
in String arg);
Run Code Online (Sandbox Code Playgroud) 我正在使用" android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT
"意图获取特定于设备的AT命令.但是当我从MY Bluetooth Kit发送AT命令时,广播记录器没有触发.
当我AT+CHUP\r
从我的工具包发送时,android在内部工作并断开呼叫.但是当我AT+XEVENT=foo,3\r
从套件发送但是我没有收到接收器中的任何东西.
帮帮我
所以有很多与此问题相关的SO帖子 -
其他职位 -
尝试从反射到没有反思但没有工作的一切 -
if(!mDeviceAddress.equals("") && BluetoothAdapter.checkBluetoothAddress(mDeviceAddress))
{
Log.i(TAG, "Remote Device Name "+mDeviceName);
bdDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
getConnected(bdDevice);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void getConnected(BluetoothDevice bdDevice)
{
if(bdDevice == null)
{
setSetting("STATUS", "Disconnected");
Toast.makeText(getActivity(),
"Unable to get Remote Device!", Toast.LENGTH_SHORT).show();
return;
}
else
{
Log.i(TAG, "Connecting Address--"+ bdDevice.getAddress());
boolean isConnected = createInsecureRfcommSocket(bdDevice, 1);
if(!isConnected)
{
for(int i=2;i<4;i++)
{
if(!isConnected)
isConnected = createInsecureRfcommSocket(bdDevice, i);
else
break;
}
}
if(isConnected)
{
Log.i(TAG, "Connected Socket");
setSetting("STATUS", "Connected");
mConnectedThread = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试开发一个应用程序来扫描 BLE 设备。但是,它只扫描一次。我尝试使用 while 循环来循环它,但它挂在那里。扫描部分在继续功能:
package com.example.user.myfriend;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends ActionBarActivity {
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello();
}
public void hello() {
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, …
Run Code Online (Sandbox Code Playgroud) 使用Kontakt SDK,我实现了一个简单的应用程序来监视和调整我所在区域的Kontakt信标.我注意到所报告的信标的距离大幅波动(例如,信标确实在10米处,但它返回为2米并且可能在距离上相当显着地跳跃),即使信标不是那么遥远.我已经浏览了文档并使用了信标硬件方面的所有不同变量(tx功率/频率),SDK方面(扫描模式和相关)并尝试了多个设备 - 我似乎没有做任何事情来提高准确性.只是想知道是否有人能够指出我可能成为问题的正确方向.
我知道信标信号可以根据环境的许多因素和接收设备本身的硬件而显着变化.但是,当我似乎没有办法获得一致的距离信息时,我很遗憾如何创建基于信标准确接近的用户体验?
任何类型的指导,输入或建议将不胜感激.谢谢.
android bluetooth bluetooth-lowenergy android-bluetooth ibeacon
我希望使用 ScanFilter 来搜索一组蓝牙设备。我知道所有这些设备的地址都以 00:A0:50 开头,然后最后 6 位数字各不相同,所以所有地址看起来都是 00:A0:50:XX:XX:XX。我正在寻找一种使用setDeviceAddress查找地址以这 6 位数字开头的设备的方法。这需要一个字符串作为输入。相关代码如下。
ScanFilter cypressFilter = new ScanFilter().Builder()
//we know that their mac address will always start with 00:A0:50
//so we should filter out any devices without that
.setDeviceAddress(/* Address string goes here */)
.build();
Run Code Online (Sandbox Code Playgroud)
我想我需要为此使用正则表达式之类的东西,但我对 Java、Android 和正则表达式还很陌生,我不确定是否可以将正则表达式传递给这个函数?查看文档,我想我需要一个 Pattern 或 Matcher 类来查找相关的字符串。但是,我不确定这是否适用于需要特定字符串作为输入的特定方法。我很惊讶它没有将数组作为输入,我认为这比单个 MAC 地址更常见。
我看到在 Android 5.1.1 中不推荐使用 stopLeScan() 和 startLeScan()。我在替换 stopLeScan() 和 startLeScan() 方法时遇到问题。这是我的以下代码:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
final Intent intent = new Intent(this, MainActivity.class);
//intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(MainActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
if (mScanning) {
mBluetoothAdapter.stopScan(mLeScanCallback);
mBluetoothAdapter.getBluetoothLeScanner();
mScanning = false;
}
startActivity(intent);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() { …
Run Code Online (Sandbox Code Playgroud) android bluetooth bluetooth-lowenergy android-bluetooth android-ble
我正在尝试提高 Android 蓝牙的传输速度。我尝试使用蓝牙 API 的 RFComm 套接字将 2.7MB 缓冲区从一个安卓设备传输到另一个安卓设备(见下面的代码)。大约需要 70 秒才能完成。我将此方法与通过手机附带的蓝牙功能的“共享”进行了比较。“共享”功能提供了非常好的性能(传输 2.7MB 文件约 15 秒)。
“共享”功能与使用蓝牙 API 有何不同?如何复制“共享”方法以获得优化的传输速度?
谢谢,
蓝牙API测试代码:
服务器端 - 安装在一台安卓设备上
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("DeviceName", MY_UUID);
socket = mServerSocket.accept();
mInStream = socket.getInputStream();
int totalByte = 1;
while (totalByte < 2718720)
{
int bytesAvailable = mInStream.available();
if (bytesAvailable > 0) {
totalByte += bytesAvailable;
byte[] buffer = new byte[bytesAvailable];
mInStream.read(buffer);
}
}
Run Code Online (Sandbox Code Playgroud)
客户端 - 安装在其他安卓设备上
mClientSocket = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID);
mClientSocket.connect();
mOutStream = mClientSocket.getOutputStream();
byte byteValue = …
Run Code Online (Sandbox Code Playgroud) android ×10
bluetooth ×7
android-ble ×1
hfp ×1
ibeacon ×1
java ×1
jsr82 ×1
performance ×1
regex ×1