在我的service里我开启了这样的线程
beaconManager.startMonitoringBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                        beaconManager.startRangingBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                        Thread.currentThread().sleep(20);
                        beaconManager.stopRangingBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                        Thread.currentThread().sleep(5000);
这是一个无限循环的线程(目前先不考虑退出时关闭它这方面的问题)
硬件调试条件也有了,然后我开USB调试收到这样的数据
D/ScanRecord﹕ parseFromBytes
D/ScanRecord﹕ first manudata for manu ID
 D/BluetoothLeScanner﹕ onScanResult() - ScanResult{mDevice=F6:62:22:85:83:27, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, -87, 84,。。
我现在想要把ScanResult后面花括号里的东西获取到service.java里的随便一个字符串中,请问应当怎样做?
似乎无法直接调用相关的接口嗯。。 

解决方案 »

  1.   

    import android.app.Service;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.IBinder;
    import android.os.Looper;
    import android.os.Message;
    import android.os.Process;
    import android.os.RemoteException;
    import android.util.Log;
    import android.widget.Toast;import org.altbeacon.beacon.BeaconConsumer;
    import org.altbeacon.beacon.BeaconManager;
    import org.altbeacon.beacon.BeaconParser;
    import org.altbeacon.beacon.MonitorNotifier;
    import org.altbeacon.beacon.Region;import java.util.Timer;//自定义第三方引用public class MainService extends Service implements BeaconConsumer {
        static Timer timer = null;
        private Looper mServiceLooper;
        private ServiceHandler mServiceHandler;
        private BeaconManager beaconManager;    protected static final String TAG = "MonitoringActivity";
        /**
         * 重新调整格式
         */
        public static final String IBEACON_FORMAT = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";
        /**
         * 设置兴趣UUID
         */
        public static final String FILTER_UUID = "a954afa9-6d68-11e5-bc69-782bcb71fea2";
        // Handler that receives messages from the thread
        private final class ServiceHandler extends Handler {
            public ServiceHandler(Looper looper) {
                super(looper);
            }        @Override
            public void handleMessage(Message msg) {
                // Normally we would ddo some work here, like download a file.
                // For our sample, we just sleep for 5 seconds.            // Stop the service using the startId, so that we don't stop
                // the service in the middle of handling another job
                //stopSelf(msg.arg1);        }
        }    @Override
        public void onCreate() {
            // Start up the thread running the service.  Note that we create a
            // separate thread because the service normally runs in the process's
            // main thread, which we don't want to block.  We also make it
            // background priority so CPU-intensive work will not disrupt our UI.
            HandlerThread thread = new HandlerThread("ServiceStartArguments",
                    Process.THREAD_PRIORITY_BACKGROUND);
            thread.start();        // Get the HandlerThread's Looper and use it for our Handler
            mServiceLooper = thread.getLooper();
            mServiceHandler = new ServiceHandler(mServiceLooper);
            beaconManager = BeaconManager.getInstanceForApplication(this);
            beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(IBEACON_FORMAT));
            beaconManager.bind(this);
        }    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();        // For each start request, send a message to start a job and deliver the
            // start ID so we know which request we're stopping when we finish the job        Message msg = mServiceHandler.obtainMessage();
            msg.arg1 = startId;        mServiceHandler.sendMessage(msg);        return START_STICKY;
            // If we get killed, after returning from here, restart
        }    @Override
        public IBinder onBind(Intent intent) {
            // We don't provide binding, so return null
            return null;
        }    @Override
        public void onDestroy() {
            Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
            beaconManager.unbind(this);
        }    /* 进行一次Beacon检测 */    @Override
        public void onBeaconServiceConnect() {
            beaconManager.setMonitorNotifier(new MonitorNotifier() {
                @Override
                public void didEnterRegion(Region region) {
                    Log.e(TAG, "I just saw an beacon for the first time!");
                }            @Override
                public void didExitRegion(Region region) {
                    Log.e(TAG, "I no longer see an beacon");
                }            @Override
                public void didDetermineStateForRegion(int state, Region region) {
                    Log.e(TAG, "I have just switched from seeing/not seeing beacons: " + state);
                }
            });
            new Thread() {            public void run() {                try {//开始监视
                        while (true) {
                            beaconManager.startMonitoringBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                            beaconManager.startRangingBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                            Thread.currentThread().sleep(20);
                            Log.e(TAG, "I no longer see an beacon");
                            String string = beaconManager.getBeaconParsers().toString();
                            Log.e("things Return", string);
                            Log.e("things Return", string);
                            beaconManager.stopRangingBeaconsInRegion(new Region(FILTER_UUID, null, null, null));
                            Thread.currentThread().sleep(5000);
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }            }
            }.start();    }    private void BLEonce() {    }    private void GPSonce() { /* 取得一次位置坐标 */    }    private void Axieonce() { /* 进行一次重力加速度采样 */    }
        // Device scan callback.
    }
      

  2.   

    线程间通信我是做过的,用handler机制,但问题是现在这个输出内容的线程在哪里都找不出来
    额楼上不知道为何最后重要的几逝行绿掉了囧。。
     while (true)里是我开的死循环一直检测beacon,没有这几句的话就不会有logcat里输出的onScanResult(),但具体是哪里调用了onScanResult(),找不到
    相关的类只有service,没有用到工程里的其他类。
    所以求问一下是哪里的问题
      

  3.   

    你好 我想知道  你下面这段结果是通过什么方式获取到的
    ScanResult{mDevice=F6:62:22:85:83:27, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, -87, 84,。。这一段 我弄了好久  都不知道从哪里取到数据 ??
      

  4.   

    在onScanResult(int callbackType, ScanResult result)函数中打印result就能直接得到你的那些数据。你随便定义一个String赋值就能获取到。其实我想问的问题是,为什么你的数据和我一样,获得的serviceuuid都是null,这个让我一直费解。
      

  5.   

    我也卡了下,然后发现里面其实有方法的,方法如下
     result.getDevice().getName()