使用Android蓝牙API的三星Galaxy标签中的蓝牙检测问题

使用Android蓝牙API的三星Galaxy标签中的蓝牙检测问题,android,tabs,bluetooth,galaxy,Android,Tabs,Bluetooth,Galaxy,我在使用Android bluetooth API使用Galaxy选项卡检测零级蓝牙设备时遇到问题。它根本看不到一些设备,尽管我可以用手机或电脑检测到它们。有人遇到过这个问题吗?我正在编写一个应用程序,它依赖于通过蓝牙与设备配对,在此方面的一些帮助将不胜感激。注意:由于需要访问设备日志,此解决方案将仅适用于旧的安卓操作系统 对!!我有完全相同的问题,三星Galaxy S上的abeit和LG Optimus One上的abeit。我编写了一个类,您可以重用它来修复此问题,不知道它是否能在Galax

我在使用Android bluetooth API使用Galaxy选项卡检测零级蓝牙设备时遇到问题。它根本看不到一些设备,尽管我可以用手机或电脑检测到它们。有人遇到过这个问题吗?我正在编写一个应用程序,它依赖于通过蓝牙与设备配对,在此方面的一些帮助将不胜感激。

注意:由于需要访问设备日志,此解决方案将仅适用于旧的安卓操作系统

对!!我有完全相同的问题,三星Galaxy S上的abeit和LG Optimus One上的abeit。我编写了一个类,您可以重用它来修复此问题,不知道它是否能在Galaxy选项卡上工作,但您可以尝试:

package com.yourpackagename;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;

// This class exists due to a bug in the Broadcomm bluetooth stack, which is
// used by many Android smart-phone manufacturers (LG, Samsung HTC etc.).  That
// bug prevents discovery of ALL bluetooth devices that report their Class of Device (CoD)
// code as 0x00, which prevent many SPP (Serial Port Profile) devices from working.
// 
// See: http://www.google.com/codesearch/p?hl=en#4hzE-Xyu5Wo/vendor/brcm/adaptation/dtun/dtunc_bz4/dtun_hcid.c&q=%22Device%20[%25s]%20class%20is%200x00%20-%20skip%20it.%22&sa=N&cd=1&ct=rc
// And: http://stackoverflow.com/questions/4215398/bluetooth-device-not-discoverable
// And: http://www.reddit.com/r/Android/comments/hao6p/my_experience_with_htc_support_eu_anyone_has/
//
// How to use (from your Activity class):
// 
//  (new BluetoothClassZeroDiscoveryTask(this, new BluetoothDiscoveryCallback())).execute();
//
// Where BluetoothDiscoveryCallback is a class defined e.g. in your Activity.  The call method
// will be called after the discovery task completes, and is passed the complete list
// of paired bluetooth devices, including those that are undiscoverable due to the above bug.
//
//  private class BluetoothDiscoveryCallback implements Action<ArrayList<BluetoothDevice>>
//  {
//      public void call(ArrayList<BluetoothDevice> devices)
//      {
//          // Now you have the list of ALL available devices, 
//          // including those that report class 0x00.
//      }
//  }
//
//  // Java equivalent of the built-in Action from C#.
//  public interface Action<T>
//  {
//      void call(T target);
//  }
//
public class BluetoothClassZeroDiscoveryTask extends AsyncTask<Void, Void, Void>
{
    // This is the well-known ID for bluetooth serial port profile (SPP) devices.
    public static final UUID BluetoothSerialUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private Activity _parent;
    private boolean _discoveryComplete = false;
    private Action<ArrayList<BluetoothDevice>> _callback;
    private ArrayList<BluetoothDevice> _devices = new ArrayList<BluetoothDevice>();
    private Calendar _discoveryStartTime;
    private SimpleDateFormat _logDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    private BluetoothAdapter _adapter;
    private ProgressDialog _progressDialog;

    public BluetoothClassZeroDiscoveryTask(Activity parent, Action<ArrayList<BluetoothDevice>> callback)
    {
        _callback = callback;
        _parent = parent;
        _adapter = BluetoothAdapter.getDefaultAdapter();

        IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        _parent.registerReceiver(mReceiver, foundFilter);
        IntentFilter finishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        _parent.registerReceiver(mReceiver, finishedFilter);

        // This task performs a scan for bluetooth devices, which 
        // takes ~ 12 seconds, so show an indeterminate progress bar.
        _progressDialog = ProgressDialog.show(_parent, "", "Discovering bluetooth devices...", true);
    }

    // Kicks off bluetooth discovery.
    @Override
    protected Void doInBackground(Void... params)
    {
        _discoveryStartTime = Calendar.getInstance();
        _adapter.startDiscovery();

        while (!_discoveryComplete)
        {
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException e) { }
        }

        _adapter.cancelDiscovery();
        return null;
    }

    // Provide notification of results to client.
    @Override
    protected void onPostExecute(Void result)
    {
        _progressDialog.dismiss();
        _parent.unregisterReceiver(mReceiver);
        _callback.call(_devices);
    }

    // Handler for bluetooth discovery events.
    private final BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // If it's already paired, skip it, (we'll add it after the scan completes).
                if (device.getBondState() != BluetoothDevice.BOND_BONDED)
                {
                    _devices.add(device);
                }
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                // Add all already-paired devices to the list.
                for (BluetoothDevice device : _adapter.getBondedDevices())
                {
                    _devices.add(device);
                }

                // Trawl through the logs to find any devices that were skipped >:(
                try
                {
                    Process process = Runtime.getRuntime().exec("logcat -d -v time *:E");
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));

                    String line;
                    Pattern pattern = Pattern.compile("(.{18}).*\\[(.+)\\] class is 0x00 - skip it.");
                    while ((line = bufferedReader.readLine()) != null)
                    {
                        Matcher matcher = pattern.matcher(line);
                        if (matcher.find())
                        {
                            // Found a blocked device, check if it was newly discovered.
                            // Android log timestamps don't contain the year!?
                            String logTimeStamp = Integer.toString(_discoveryStartTime.get(Calendar.YEAR)) + "-" + matcher.group(1);
                            Date logTime = null;
                            try
                            {
                                logTime = _logDateFormat.parse(logTimeStamp);
                            }
                            catch (ParseException e) { }

                            if (logTime != null)
                            {
                                if (logTime.after(_discoveryStartTime.getTime()))
                                {
                                    // Device was discovered during this scan,
                                    // now we want to get the name of the device.
                                    String deviceAddress = matcher.group(2);
                                    BluetoothDevice device = _adapter.getRemoteDevice(deviceAddress);

                                    // In order to get the name, we must attempt to connect to the device.
                                    // This will attempt to pair with the device, and will ask the user
                                    // for a PIN code if one is required.
                                    try
                                    {
                                        BluetoothSocket socket = device.createRfcommSocketToServiceRecord(BluetoothSerialUuid);
                                        socket.connect();
                                        socket.close();
                                        _devices.add(device);
                                    }
                                    catch (IOException e) { }
                                }
                            }
                        }
                    }
                }
                catch (IOException e) {}

                _discoveryComplete = true;
            }
        }
    };
}
package com.yourpackagename;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.Date;
导入java.util.UUID;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.AsyncTask;
//此类的存在是由于Broadcomm bluetooth堆栈中的一个错误,即
//被许多安卓智能手机制造商(LG、三星HTC等)使用。那个
//错误阻止发现所有报告其设备类别(CoD)的蓝牙设备
//代码为0x00,这会阻止许多SPP(串行端口配置文件)设备工作。
// 
//见:http://www.google.com/codesearch/p?hl=en#4hzE-Xyu5Wo/vendor/brcm/adaption/dtun/dtunc_bz4/dtun_hcid.c&q=%22设备%20[%25s]%20类%20为%200x00%20-%20skip%20it.%22&sa=N&cd=1&ct=rc
//以及:http://stackoverflow.com/questions/4215398/bluetooth-device-not-discoverable
//以及:http://www.reddit.com/r/Android/comments/hao6p/my_experience_with_htc_support_eu_anyone_has/
//
//如何使用(从活动课中):
// 
//(新的BluetoothClassZeroDiscoveryTask(这是新的BluetoothDiscoveryCallback()).execute();
//
//其中,Bluetooth DiscoveryCallback是一个在您的活动中定义的类。调用方法
//将在发现任务完成后调用,并传递完整列表
//配对的蓝牙设备,包括由于上述错误而无法发现的设备。
//

//私有类Bluetooth DiscoveryCallback实施操作

谢谢您的帮助,您真的解决了我的问题。这可能不是理想的解决方案,但这是我遇到的唯一一个。你知道对0x00类设备的过滤是Android蓝牙堆栈的一部分,还是与硬件相关(三星、LC等供应商的实现,因此与设备相关)?我相信不同供应商之间的蓝牙堆栈是不同的,例如三星、LG、HTC似乎都使用Broadcom堆栈,但是,如果销售商选择使用不同的蓝牙硬件,还有其他的蓝牙协议栈。据我所知,谷歌Nexus手机不过滤0x00。(顺便说一句,如果您的问题已解决,请将此标记为已回答-谢谢。)您是否可以单击答案评级附近(答案左侧)的勾号?链接已关闭。有人有解决办法吗?