Android 蓝牙开发-分包处理

1 数据分包处理

① android 数据大于20字节会自动分包接收;

② 分包间隔测试时间约50ms,

③ 实际处理:比较前一包和后一包的时间间隔,如果大于100ms则证明整帧数据发送完成,发送广播给内容显示端;

1.1 利用线程

public class MainActivity extends BaseActivity {
    private int mConnectTimes=0;
    private LocalBroadcastManager mLocalBroadcastManager;
    private  long mRevTimeMsPre=System.currentTimeMillis();
    private  int mRevDataOneFrameIndex=0;
    private boolean mRevDelayThreadIsRun=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    protected  void BleReadDataNotify(BleDevice bleDevice,BluetoothGattCharacteristic characteristic)
    {
        BleManager.getInstance().notify(
                bleDevice,
                characteristic.getService().getUuid().toString(),
                characteristic.getUuid().toString(),
                new BleNotifyCallback() {
                    @Override
                    public void onNotifySuccess() {
                        Log.d(TAG, "onNotifySuccess: ");
                    }

                    @Override
                    public void onNotifyFailure(BleException exception) {
                        Log.d(TAG, "onNotifyFailure: ");
                    }

                    @Override
                    public void onCharacteristicChanged(byte[] data) {
                        int signal=0;
                        long mRevTimeMsCurrent=System.currentTimeMillis();
                        long revGapTimeMs=mRevTimeMsCurrent-mRevTimeMsPre;

                        mRevTimeMsPre=mRevTimeMsCurrent;
                        if(revGapTimeMs>80)
                        {
                            mRevDataOneFrameIndex=0;
                            signal=mRevDataOneFrameIndex;
                            Log.d(TAG, "onCharacteristicChanged: new Frame");
                        }
                        else
                        {
                            mRevDataOneFrameIndex=mRevDataOneFrameIndex+1;
                            signal=mRevDataOneFrameIndex;
                        }
                        Log.d(TAG, "Notify onCharacteristicChanged: "+DataProcess.bytes2Str(data)+"  "+data.length+" "+revGapTimeMs);

                        DataProcess.getFullFrame(data,data.length);
                        if(!mRevDelayThreadIsRun) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    long mRevTimeMsCurrent = System.currentTimeMillis();
                                    Log.d(TAG, "run: ");
                                    mRevDelayThreadIsRun=true;
                                    while (System.currentTimeMillis() - mRevTimeMsPre < 100) {
                                        try {
                                            Thread.sleep(10);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        Log.d(TAG, "run while: ");

                                    }
                                    Message mes = new Message();
                                    mes.what = 1;
                                    mHandler.sendMessage(mes);

                                    Intent intent=new Intent("com.example.myble.LOCAL_BROADCAST");
                                    Bundle bundle=new Bundle();
                                    bundle.putInt("signal",1);
                                    intent.putExtra("data",bundle);
                                    mLocalBroadcastManager.sendBroadcast(intent);

                                    mRevDelayThreadIsRun=false;
                                    Log.d(TAG, "run: exit");
                                }
                            }).start();
                        }
                    }
                }
        );
    }
    public Handler mHandler=new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 1:
                    Log.d(TAG, "handleMessage: ");
                    break;
                default:
                    break;
            }
        }
    };



}

1.2 利用Timer + TimerTask

注意点:

1 timer.cancel 后必须要新建Timer 和 TimerTaks不然没法启动

//蓝牙读取函数
public void onCharacteristicChanged(byte[] data) {
	/***接收函数 省略*****/
	
	
	if(mTestTimer!=null)
	{
		mTestTimer.cancel();
		mTestTimer.purge();
	}
	mTestTimer = new Timer();
	mTestTimer.schedule(new TimerTask() {
		@Override
		public void run() {
			Log.d(TAG, "Timeout Sent Message: "+System.currentTimeMillis());
			Message mes = new Message();
			mes.what = 1;
			mHandler.sendMessage(mes);

			Intent intent=new Intent("com.example.myble.LOCAL_BROADCAST");
			Bundle bundle=new Bundle();
			bundle.putInt("signal",1);
			intent.putExtra("data",bundle);
			mLocalBroadcastManager.sendBroadcast(intent);
		}
	},200);
}

3 调试结果

2022-04-19 15:38:23.796 22340-22340/com.example.myble D/MainActivity: onCharacteristicChanged: new Frame
2022-04-19 15:38:23.796 22340-22340/com.example.myble D/MainActivity: Notify onCharacteristicChanged: 123456789a123456789a  20 193041650353903796
2022-04-19 15:38:23.823 22340-22340/com.example.myble D/MainActivity: Notify onCharacteristicChanged: 123456789a123456789a  20 271650353903823
2022-04-19 15:38:23.840 22340-22340/com.example.myble D/MainActivity: Notify onCharacteristicChanged: 123456789a123456789a  20 161650353903839
2022-04-19 15:38:23.850 22340-22340/com.example.myble D/MainActivity: Notify onCharacteristicChanged: 1  1 111650353903850
2022-04-19 15:38:24.059 22340-22429/com.example.myble D/MainActivity: Timeout Sent Message: 1650353904058
2022-04-19 15:38:24.059 22340-22340/com.example.myble D/MainActivity: handleMessage: 

你可能感兴趣的:(android)