色yeye在线视频观看_亚洲人亚洲精品成人网站_一级毛片免费播放_91精品一区二区中文字幕_一区二区三区日本视频_成人性生交大免费看

當前位置:首頁 > 學習資源 > 講師博文 > 智能手環客戶端詳細設計

智能手環客戶端詳細設計 時間:2018-10-28      來源:華清遠見

1.1 客戶端簡介

1.1.1 界面操作

進入頁面后,會自動進行掃描,以發現周圍可用藍牙設備。

在搜索藍牙設備的多層中,會出現圓環形進度條,當設定的搜索時間到了之后,或者我們選中了被掃描到的設備,該進度條消失,“停止掃描”轉換會“掃描”。

點擊掃描到的設備進入控制設備控制頁面。

這個頁面是我們的主要控制界面,我們可以在這個界面看到手環為我們提供的一些數據。

這個下拉菜單可以同過點擊也可以通過設備的menu按鍵獲得,主要用來控制計步器。

“斷開連接”的按鈕的作用是顯示現在設備的連接情況,如果不是已經連接了設備,那么可以看到“連接”按鈕,可以通過“連接”按鈕來實現和藍牙設備的連接,如果是已經連接了的設備,可以看到“斷開連接”,可以通過“斷開連接”來斷開和藍牙設備的連接。

設置時間按鈕,可以將我們的設備上的時間,同步到藍牙手環上。

1.1.2 工程結構

Src為java文件目錄。

Res為資源文件目錄。

AndroidManifest.xml為配置文件。

在src目錄下:

Acticity包中,是工程的兩個Activity活動相關的代碼。

Service包中,是工程啟動的服務相關代碼。

Tools包中,是工具類相關代碼。

UI包中,是自定義界面相關代碼。

在資源文件夾中,包含了Layout,values,Drawable等資源文件夾:

其中layout中是布局文件,menu中,是菜單欄的布局文件,values中包括color,string,styles等設置。

1.2 代碼分析

1.2.1 界面代碼分析

本工程一共有兩個主要顯示界面,分別為掃描設備,和設備控制,通過兩個活動—Activity來進行設置與控制。

掃描-----界面ScanningActivity

在onCreate中設定標題。

Java Code

public void onCreate(Bundle savedInstanceState){

getActionBar().setTitle(R.string.title_devices);

}

在掃描的時候,menu顯示為“停止掃描”,并顯示圓環進度條以提醒等待;在掃描結束時,menu顯示為“掃描”。在界面中使用ListView展示掃描到的設備。

Java Code

/**

* 創建Menu菜單欄

*/

@Override

public boolean onCreateOptionsMenu(Menu menu)

{

getMenuInflater().inflate(R.menu.main, menu);

if (!mScanning)

{

menu.findItem(R.id.menu_stop).setVisible(false);

menu.findItem(R.id.menu_scan).setVisible(true);

menu.findItem(R.id.menu_refresh).setActionView(null);

} else{

menu.findItem(R.id.menu_stop).setVisible(true);

menu.findItem(R.id.menu_scan).setVisible(false);

menu.findItem(R.id.menu_refresh).setActionView(

R.layout.actionbar_indeterminate_progress);

}

return true;

}

/**

* Menu菜單欄被選擇時

*/

@Override

public boolean onOptionsItemSelected(MenuItem item)

{

switch (item.getItemId())

{

case R.id.menu_scan:

mLeDeviceListAdapter.clear();

scanLeDevice(true);

break;

case R.id.menu_stop:

scanLeDevice(false);

break;

}

return true;

}

ListView顯示掃描到的設備。

Java Code

@Override

protected void onResume(){

super.onResume();

if (!mBluetoothAdapter.isEnabled()){

if (!mBluetoothAdapter.isEnabled()){

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

}

mLeDeviceListAdapter = new LeDeviceListAdapter();

setListAdapter(mLeDeviceListAdapter);

scanLeDevice(true);

}

點擊ListView中的設備跳轉到控制界面。

Java Code

@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, DeviceControlActivity.class);

intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());

intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());

if (mScanning) {

mBluetoothAdapter.stopLeScan(mLeScanCallback);

mScanning = false;

}

startActivity(intent);

}

設備控制-------界面DeviceControlActivity

在Activity中設置主界面:

Java Code

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_device_control);

initShow();

btnTime.setOnClickListener(this);

final Intent intent = getIntent();

mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);

mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);

Toast.makeText(this, mDeviceAddress, Toast.LENGTH_SHORT).show();

getActionBar().setTitle(mDeviceName);

getActionBar().setDisplayHomeAsUpEnabled(true);

}

菜單欄包括兩部分,一部分是顯示“連接”或“斷開連接”,一部分是隱藏的用以對計步功能進行設置。

Java Code

@Override

public boolean onCreateOptionsMenu(Menu menu){

getMenuInflater().inflate(R.menu.device_control, menu);

if (connected) {

menu.findItem(R.id.menu_connect).setVisible(false);

menu.findItem(R.id.menu_disconnect).setVisible(true);

}else{

menu.findItem(R.id.menu_connect).setVisible(true);

menu.findItem(R.id.menu_disconnect).setVisible(false);

}

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item){

int id = item.getItemId();

switch (id){

case R.id.setting_gogal:

showGogalDialog();

break;

case R.id.setting_reset:

showResetDialog();

break;

case R.id.setting_stop:

showStopDialog();

break;

case R.id.menu_connect:

mBleService.connect(mDeviceAddress);

return true;

case R.id.menu_disconnect:

mBleService.disconnect();

return true;

case android.R.id.home:

onBackPressed();

return true;

default:

break;

}

return super.onOptionsItemSelected(item);

}

1.2.2 藍牙連接代碼分析

掃描

藍牙連接建立的過程中,首先要先掃描到設備。

在ScanningActivity中:

Java Code

public class ScanningActivity extends ListActivity{

private LeDeviceListAdapter mLeDeviceListAdapter;

private BluetoothAdapter mBluetoothAdapter;

private boolean mScanning;

private Handler mHandler;

private static final int REQUEST_ENABLE_BT = 1;

// Stops scanning after 10 seconds.

private static final long SCAN_PERIOD = 10000;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mHandler = new Handler();

//PackageManager.FEATURE_BLUETOOTH_LE

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){

Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();

finish();

}

//BluetoothManager

final BluetoothManager bluetoothManager =

(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = bluetoothManager.getAdapter();

//BluetoothAdapter

if (mBluetoothAdapter == null){

Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();

finish();

return;

}

}

private void scanLeDevice(final boolean enable){

if (enable){

mHandler.postDelayed(new Runnable(){

@Override

public void run(){

mScanning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

invalidateOptionsMenu();

}

}, SCAN_PERIOD);

mScanning = true;

mBluetoothAdapter.startLeScan(mLeScanCallback);

}else{

mScanning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

}

invalidateOptionsMenu();

}

private BluetoothAdapter.LeScanCallback mLeScanCallback =

new BluetoothAdapter.LeScanCallback(){

@Override

public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord){

runOnUiThread(new Runnable(){

@Override

public void run(){

mLeDeviceListAdapter.addDevice(device);

mLeDeviceListAdapter.notifyDataSetChanged();

}

});

}

};

}

連接藍牙

在掃描結束的時候,點擊ListView上的設備名稱的時候,將設備MAC通過Intent傳遞給DeviceControlActivity,在控制頁面啟動BLEService服務,控制設備連接。

Java Code

public class DeviceControlActivity extends Activity implements View.OnClickListener

{

private final String TAG = "Device";

private final Double G = 9.8;

public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";

public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";

private static String UUID_date = "0000180f-0000-1000-8000-00805f9b34fb";

private static String UUID_D = "00002a19-0000-1000-8000-00805f9b34fb";

private String UUID_write = "00001802-0000-1000-8000-00805f9b34fb";

private String UUID_W = "00002a06-0000-1000-8000-00805f9b34fb";

private Phone mPhone;

private BLEService mBleService;

private String mDeviceAddress;

private String mDeviceName;

private int xBefore, yBefore, zBefore;

private CircleBar mCircleBar;

private Button btnTime;

private TextView txvX, txvY, txvZ, txvPower;

private EditText gogalInput;

private boolean connected = false;

private boolean first = true;

private BluetoothGattCharacteristic mNotifyCharacteristic;

protected void onCreate(Bundle savedInstanceState){

//...

super.onCreate(savedInstanceState);

//bindService啟動服務,注冊廣播接收器實現服務與活動之間的通信

Intent gattIntent = new Intent(this, BLEService.class);

bindService(gattIntent, mServiceConnection, BIND_AUTO_CREATE);

registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

//實例化一個電話模型。

mPhone = new Phone(this);

}

@Override

protected void onResume(){

super.onResume();

registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

if (mBleService != null){

final boolean result = mBleService.connect(mDeviceAddress);

Log.d(TAG, "Connect request result=" + result);

}

}

//服務連接

private final ServiceConnection mServiceConnection = new ServiceConnection(){

@Override

public void onServiceDisconnected(ComponentName name){

}

@Override

public void onServiceConnected(ComponentName name, IBinder service){

mBleService = ((BLEService.LocalBinder) service).getService();

if (!mBleService.initialize()){

Log.e(TAG, "unable to initilize Bluetooth Service");

finish();

}

mBleService.connect(mDeviceAddress);

}

};

}

BLEService服務代碼如下:

Java Code

public class BLEService extends Service{

private BluetoothManager mBluetoothManager;

private BluetoothAdapter mBluetoothAdapter;

private String mBluetoothDeviceAddress;

public BluetoothGatt mBluetoothGatt;

public final static String ACTION_TIME_SETTING = "com.farsight.bluetooth.le.ACTION_TIME_SETTING";

public final static String ACTION_GATT_CONNECTED = "com.farsight.bluetooth.le.ACTION_GATT_CONNECTED";

public final static String ACTION_GATT_DISCONNECTED = "com.farsight.bluetooth.le.ACTION_GATT_DISCONNECTED";

public final static String ACTION_GATT_SERVICES_DISCOVERED = "com.farsight.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";

public final static String ACTION_DATA_AVAILABLE = "com.farsight.bluetooth.le.ACTION_DATA_AVAILABLE";

public final static String EXTRA_DATA = "com.farsight.bluetooth.le.EXTRA_DATA";

public final static UUID UUID_FARSIGHT_SMART_WRIST = UUID.fromString(GattAttributes.FARSIGHT_SMART_WRIST);

/**

* 回調函數

*/

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback(){

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){

String intentAction;

if (newState == BluetoothProfile.STATE_CONNECTED){

mBluetoothGatt.getServices();

intentAction = ACTION_GATT_CONNECTED;

mConnectionState = STATE_CONNECTED;

broadcastUpdate(intentAction);

Log.i(TAG, "Connected to GATT server.");

// Attempts to discover services after successful connection.

Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());

}else if (newState == BluetoothProfile.STATE_DISCONNECTED){

intentAction = ACTION_GATT_DISCONNECTED;

mConnectionState = STATE_DISCONNECTED;

Log.i(TAG, "Disconnected from GATT server.");

broadcastUpdate(intentAction);

}

}

//發現新服務

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status){

if (status == BluetoothGatt.GATT_SUCCESS){

broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);

}else{

Log.w("DSTTTTTTTTT", "onServicesDiscovered received: " + status);

}

}

//讀到特征設備

@Override

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){

if (status == BluetoothGatt.GATT_SUCCESS){

broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

}

}

@Override

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){

broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

}

};

/**

* 通過廣播發送數據

*/

private void broadcastUpdate(final String action){

final Intent intent = new Intent(action);

sendBroadcast(intent);

}

/**

* 通過廣播發送數據

*/

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic){

final Intent intent = new Intent(action);

// For all profiles, writes the data formatted in HEX.

final byte[] data = characteristic.getValue();

Log.d(TAG, data.length + "");

if (data != null && data.length > 0){

final StringBuilder stringBuilder = new StringBuilder(data.length);

for (byte byteChar : data)

stringBuilder.append(String.format("%02X ", byteChar));

// new String(data) + "\n" +

intent.putExtra(EXTRA_DATA, stringBuilder.toString());

}

sendBroadcast(intent);

}

if (mBluetoothManager == null){

mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

if (mBluetoothManager == null){

Log.e(TAG, "Unable to initialize BluetoothManager.");

return false;

}

}

mBluetoothAdapter = mBluetoothManager.getAdapter();

if (mBluetoothAdapter == null){

Log.e(TAG, "Unable to obtain a BluetoothAdapter.");

return false;

}

return true;

}

/**

* 連接

*/

public boolean connect(final String address){

if (mBluetoothAdapter == null || address == null){

Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");

return false;

}

if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null){

Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");

if (mBluetoothGatt.connect()){

mConnectionState = STATE_CONNECTING;

return true;

}else{

return false;

}

}

final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

if (device == null){

Log.w(TAG, "Device not found. Unable to connect.");

return false;

}

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

Log.d(TAG, "Trying to create a new connection.");

mBluetoothDeviceAddress = address;

mConnectionState = STATE_CONNECTING;

return true;

}

/**

* 可通知

*/

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled){

if (mBluetoothAdapter == null || mBluetoothGatt == null){

Log.w(TAG, "BluetoothAdapter not initialized");

return;

}

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

if (UUID_FARSIGHT_SMART_WRIST.equals(characteristic.getUuid())){

BluetoothGattDescriptor descriptor = characteristic

.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

mBluetoothGatt.writeDescriptor(descriptor);

}

}

/**

* 所有服務列表

*/

public List getSupportedGattServices(){

if (mBluetoothGatt == null)

return null;

return mBluetoothGatt.getServices();

}

1.2.3 信號處理

從藍牙設備接收數據

這里我們使用設備接收來自藍牙的通知即Notification信號,所以我們需要讓我們的設備讀取相應特征值的可通知信號。

在BLEService中:

Java Code

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled)

{

if (mBluetoothAdapter == null || mBluetoothGatt == null)

{

Log.w(TAG, "BluetoothAdapter not initialized");

return;

}

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

if (UUID_FARSIGHT_SMART_WRIST.equals(characteristic.getUuid()))

{

BluetoothGattDescriptor descriptor = characteristic

.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

mBluetoothGatt.writeDescriptor(descriptor);

}

}

在DeviceControlActivity中,通過廣播接收器接收來自BLEService中的廣播發出的信號:

Java Code

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver(){

@Override

public void onReceive(Context context, Intent intent)

{

final String action = intent.getAction();

Log.d(TAG, action);

if (BLEService.ACTION_GATT_CONNECTED.equals(action))

{

connected = true;

invalidateOptionsMenu();

}

else if (BLEService.ACTION_GATT_DISCONNECTED.equals(action))

{

connected = false;

invalidateOptionsMenu();

}

else if (BLEService.ACTION_GATT_SERVICES_DISCOVERED.equals(action))

{

Log.d(TAG, "received Discoveryed");

if (first)

{

first = false;

}

getcharacteristic(mBleService.getSupportedGattServices());

}

else if (BLEService.ACTION_DATA_AVAILABLE.equals(action))

{

String data = intent.getStringExtra(BLEService.EXTRA_DATA);

setTribleAndPower(data);

getcharacteristic(mBleService.getSupportedGattServices());

Log.d("DATAIS", data);

}

}

};

當發現服務的時候,調用getcharacteristic(mBleService.getSupportedGattServices())以判斷特征值是否是我們需要的。

Java Code

private void getcharacteristic(List gattServices){

String uuid = null;

Log.d(TAG, "++++++++++++++++++++++++++++++++++++++++++++");

for (BluetoothGattService gattService : gattServices) {

uuid = gattService.getUuid().toString();

Log.d(TAG, uuid);

if (uuid.equalsIgnoreCase(UUID_date)) {

List gattCharacteristics = gattService.getCharacteristics();

for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics)

uuid = gattCharacteristic.getUuid().toString();

Log.d(TAG, uuid + "characteristic");

if (uuid.equalsIgnoreCase(UUID_D)) {

final int charaProp = gattCharacteristic.getProperties();

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {

// If there is an active notification on a

// characteristic, clear

if (mNotifyCharacteristic != null) {

mBleService.setCharacteristicNotification(mNotifyCharacteristic, false);

mNotifyCharacteristic = null;

}

mBleService.readCharacteristic(gattCharacteristic);

}

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {

mNotifyCharacteristic = gattCharacteristic;

mBleService.setCharacteristicNotification(gattCharacteristic, true); }

}else{

continue;

}

}

}

}

當接收到ACTION_DATA_AVAILABLE廣播的時候,讀出廣播中的數據,并通過數據對界面進行操作。

向藍牙設備發送數據

電話狀態監聽類代碼:

Java Code

package com.farsight.lastsmartwrist.tools;

import android.annotation.SuppressLint;

import android.content.Context;

import android.database.ContentObserver;

import android.database.Cursor;

import android.net.Uri;

import android.os.Handler;

import android.provider.CallLog;

import android.provider.CallLog.Calls;

import android.telephony.PhoneStateListener;

import android.telephony.TelephonyManager;

public class Phone

{

private boolean first = true;

public byte msgCount = 0;

public byte phoneCount = 0;

private Context context = null;

public BufferTool cmdBuf = null;

public Phone(Context con)

{

context = con;

cmdBuf = new BufferTool(1024);

TelephonyManager teleMgr = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

teleMgr.listen(new MyPhoneStateListener(),

PhoneStateListener.LISTEN_CALL_STATE);

registerObserver();

init();

}

// 剛開始就發送現有的數據

void init()

{

phoneCount = (byte) readMissCall();

cmdBuf.addBuffer((byte) 'a', 1, phoneCount);

msgCount = (byte) getNewSmsCount();

cmdBuf.addBuffer((byte) 'c', 1, msgCount);

}

public void Quit()

{

unregisterObserver();

}

// 監聽來電

class MyPhoneStateListener extends PhoneStateListener

{

@Override

public void onCallStateChanged(int state, String incomingNumber)

{

if (state == TelephonyManager.CALL_STATE_RINGING)

{

cmdBuf.addBuffer((byte) 'b', incomingNumber.getBytes().length,

incomingNumber.getBytes());

}

else if (state == TelephonyManager.CALL_STATE_IDLE)

{

int tmp = readMissCall();

if (first == true)

{

phoneCount = (byte) (tmp);

first = false;

}

else

{

phoneCount = (byte) (tmp + 1);

}

cmdBuf.addBuffer((byte) 'a', 1, phoneCount);

}

}

}

// 監聽短信

public ContentObserver newMmsContentObserver = new ContentObserver(

new Handler())

{

public void onChange(boolean selfChange)

{

if (cmdBuf.getVolume() == 0)

{

cmdBuf.addBuffer((byte) 'd', 1, (byte) (getNewSmsCount() + 1));// 發送短信通知

}

}

};

// 注冊監聽者

@SuppressLint("NewApi")

public void registerObserver()

{

unregisterObserver();

context.getContentResolver().registerContentObserver(

Uri.parse("content://sms"), true, newMmsContentObserver);

}

// 取消注冊

public void unregisterObserver()

{

try

{

if (newMmsContentObserver != null)

{

context.getContentResolver().unregisterContentObserver(

newMmsContentObserver);

}

}

catch (Exception e)

{

}

}

// 獲得短信條數

public int getNewSmsCount()

{

int result = 0;

Cursor csr = context.getContentResolver().query(

Uri.parse("content://sms"), null, "type = 1 and read = 0",

null, null);

if (csr != null)

{

result = csr.getCount();

csr.close();

return result;

}

else

{

return 0;

}

}

// 獲得電話個數

public int readMissCall()

{

int result = 0;

Cursor cursor = context.getContentResolver().query(

CallLog.Calls.CONTENT_URI, new String[] { Calls.TYPE },

" type=? and new=?",

new String[] { Calls.MISSED_TYPE + "", "1" }, "date desc");

if (cursor != null)

{

result = cursor.getCount();

cursor.close();

return result;

}

else

{

return 0;

}

}

}

要發送的數據通過BuffTools來設置:

Java Code

package com.farsight.lastsmartwrist.tools;

mport java.util.Collection;

import java.util.HashMap;

import android.annotation.SuppressLint;

public class BufferTool

{

private int volume = 0;

private HashMap buffer = null;

@SuppressLint("UseSparseArrays")

public BufferTool(int dataVolume)

{

volume = dataVolume;

buffer = new HashMap(volume);

}

public synchronized Boolean addBuffer(byte[] data)

{

if (data != null)

{

if (buffer.size() < volume)

{

buffer.put(buffer.size(), data);

return true;

}

else

{

return false;

}

}

else

{

return false;

}

}

public synchronized Boolean addBuffer(int kind, int num, int msg)

{

byte[] cmdd = { (byte) kind, (byte) num, (byte) msg };

if (buffer.size() < volume)

}

else

{

return false;

}

}

public synchronized Boolean addBuffer(int kind, int num, byte[] msg)

{

byte[] cmdd = new byte[msg.length + 2];

cmdd[0] = (byte) kind;

cmdd[1] = (byte) num;

System.arraycopy(msg, 0, cmdd, 2, msg.length);

if (buffer.size() < volume)

{

buffer.put(buffer.size(), cmdd);

return true;

}

else

{

return false;

}

}

public synchronized Boolean addBuffer(int cmd)

{

byte[] cmdd = { (byte) cmd };

if (buffer.size() < volume)

{

buffer.put(buffer.size(), cmdd);

return true;

}

else

{

return false;

}

}

public synchronized byte[] getBuffer()

{

byte[] data = null;

if (buffer.size() > 0)

{

data = buffer.get(buffer.size() - 1);

buffer.remove(buffer.size() - 1);

}

else

{

}

return data;

}

public synchronized byte[][] getBuffer(int num)

{

byte[][] data = new byte[num][];

for (int i = 0; i < num; i++)

{

if (buffer.size() > 0)

{

data[i] = buffer.get(0);

buffer.remove(0);

}

else

{

data[i] = null;

break;

}

}

return data;

}

public synchronized byte[][] getAllBuffer()

{

if (buffer.isEmpty() == false)

{

int num = 0;

Collection Col = buffer.values();

int vol = Col.size();

byte[][] data = new byte[vol + 1][];

for (byte[] i : Col)

{

data[num] = i;

num++;

}

data[num] = null;

buffer.clear();

return data;

}

else

{

return null;

}

}

public synchronized int getVolume()

{

return buffer.size();

}

}

和接收數據時不一樣的是,我們在發現服務和特征值之后,就應該去查看Phone實例中是否有未傳輸出去的數據,如果有,而且服務和特征值是我們要發送的設備的話,就要進行數據傳送。

Java Code

private void getcharacteristic(List gattServices)

{

String uuid = null;

Log.d(TAG, "++++++++++++++++++++++++++++++++++++++++++++");

for (BluetoothGattService gattService : gattServices)

{

uuid = gattService.getUuid().toString();

Log.d(TAG, uuid);

if (uuid.equalsIgnoreCase(UUID_date))

{

。。。。

}

else if (uuid.equals(UUID_write))

{

List gattCharacteristics = gattService.getCharacteristics();

for (BluetoothGattCharacteristic bluetoothGattCharacteristic : gattCharacteristics)

{

uuid = bluetoothGattCharacteristic.getUuid().toString();

final BluetoothGattCharacteristic inBluetoothGattCharacteristic = bluetoothGattCharacteristic;

if (uuid.equals(UUID_W))

{

byte[] datas = null;

if (0 != mPhone.cmdBuf.getVolume())

{

}

while (null != (datas = mPhone.cmdBuf.getBuffer()))

{

inBluetoothGattCharacteristic.setValue(datas);

mBleService.mBluetoothGatt.writeCharacteristic(inBluetoothGattCharacteristic);

}

}

else

{

continue;

}

}

}

else

{

continue;

}

}

}

上一篇:安卓開發中實用的例子

下一篇:智能手環客戶端詳細設計

戳我查看嵌入式每月就業風云榜

點我了解華清遠見高校學霸學習秘籍

猜你關心企業是如何評價華清學員的

干貨分享
相關新聞
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2024 北京華清遠見科技發展有限公司 版權所有 ,京ICP備16055225號-5京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 污污内射久久一区二区欧美日韩 | 国产第56页 | 中文字幕一区二区三区在线不卡 | 国产精品天天在线午夜更新 | 秋霞理论理论福利院久久 | 国产真实夫妇交换视频 | 国产亚洲精品久久777777美腿 | 在线aⅴ亚洲中文字幕 | JIZZJIZZ少妇亚洲水多 | 乱码午夜-极品国产内射 | 久久精品国产亚洲大片 | 精品久久久爽爽久久久AV | 天天躁日日躁狠狠躁性色AV | 性深夜免费福利视频 | 亚洲永久无码7777KKK | 伊人春色视频 | 日日夜夜免费精品视频 | 永夜星河短剧免费观看 | 成人A片色情免费观看 | 国产女人高潮抽搐叫床视频 | 国产午夜亚洲精品理论片不卡 | 亚洲AV无码国产精品久久不卡 | 久久天天躁狠狠躁夜夜AV | 久久久久亚洲AV综合仓井空 | 护士张开腿被奷日出白浆 | 中文字幕第8页 | 99久久精品无码一区二区三区 | 国产精品无圣光一区二区 | 亚洲aⅴ综合av国产八av | 色黄啪啪网18以下勿进 | 成全视频在线观看免费高清在线观看 | 俺たちの熟女纱香60歳 | 中文字幕乱伦视频 | 狠狠五月激情六月丁香 | 在线观看亚洲AV日韩A∨ | 久久久久久久久久久免费精品 | 奶头好大揉着好爽GIF动态图 | 日产无码精品一区二区三区 | 久久精品国产只有精品2020 | 最新国产成人av网站网址麻豆 | 国产在线孕妇孕交 |