diff --git a/app/app.iml b/app/app.iml index 9c3c1b5..fd1fe07 100644 --- a/app/app.iml +++ b/app/app.iml @@ -84,6 +84,7 @@ + diff --git a/app/src/androidTest/java/com/zhaoxiaodan/mibanddemo/ApplicationTest.java b/app/src/androidTest/java/com/zhaoxiaodan/mibanddemo/ApplicationTest.java index 00a3e07..3078966 100644 --- a/app/src/androidTest/java/com/zhaoxiaodan/mibanddemo/ApplicationTest.java +++ b/app/src/androidTest/java/com/zhaoxiaodan/mibanddemo/ApplicationTest.java @@ -6,10 +6,8 @@ /** * Testing Fundamentals */ -public class ApplicationTest extends ApplicationTestCase -{ - public ApplicationTest() - { - super(Application.class); - } +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cf3bfaf..fffc026 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,16 +1,16 @@ > + android:versionCode="1" + android:versionName="1.0">> + android:label="@string/app_name"> + android:label="@string/app_name"> @@ -19,9 +19,9 @@ - + android:label="@string/app_name"> + diff --git a/app/src/main/java/com/zhaoxiaodan/mibanddemo/MainActivity.java b/app/src/main/java/com/zhaoxiaodan/mibanddemo/MainActivity.java index 2c91d24..bc0db75 100644 --- a/app/src/main/java/com/zhaoxiaodan/mibanddemo/MainActivity.java +++ b/app/src/main/java/com/zhaoxiaodan/mibanddemo/MainActivity.java @@ -1,9 +1,5 @@ package com.zhaoxiaodan.mibanddemo; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Arrays; - import android.app.Activity; import android.app.ProgressDialog; import android.bluetooth.BluetoothDevice; @@ -30,286 +26,212 @@ import com.zhaoxiaodan.miband.model.UserInfo; import com.zhaoxiaodan.miband.model.VibrationMode; -public class MainActivity extends Activity -{ - private static final String TAG = "==[mibandtest]=="; - private MiBand miband; - - private static final int Message_What_ShowLog = 1; - - private TextView logView ; - - private Handler handler = new Handler(Looper.getMainLooper()){ - public void handleMessage(Message m) { - switch (m.what) - { - case Message_What_ShowLog: - String text = (String)m.obj; - logView.setText(text); - break; - } - } - }; - - static final String[] BUTTONS = new String[] { - "Connect", - "showServicesAndCharacteristics", - "read_rssi", - "battery_info", - "setUserInfo", - "setHeartRateNotifyListener", - "startHeartRateScan", - "miband.startVibration(VibrationMode.VIBRATION_WITH_LED);", - "miband.startVibration(VibrationMode.VIBRATION_WITHOUT_LED);", - "miband.startVibration(VibrationMode.VIBRATION_10_TIMES_WITH_LED);", - "stopVibration", - "setNormalNotifyListener", - "setRealtimeStepsNotifyListener", - "enableRealtimeStepsNotify", - "disableRealtimeStepsNotify", - "miband.setLedColor(LedColor.ORANGE);", - "miband.setLedColor(LedColor.BLUE);", - "miband.setLedColor(LedColor.RED);", - "miband.setLedColor(LedColor.GREEN);", - "setSensorDataNotifyListener", - "enableSensorDataNotify", - "disableSensorDataNotify", - "pair", - }; - - - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - - - this.logView = (TextView)findViewById(R.id.textView); - - Intent intent=this.getIntent(); - final BluetoothDevice device = intent.getParcelableExtra("device"); - - - miband = new MiBand(this); - ListView lv = (ListView)findViewById(R.id.listView); - lv.setAdapter(new ArrayAdapter(this, R.layout.item, BUTTONS)); - lv.setOnItemClickListener(new OnItemClickListener() { - public void onItemClick(AdapterView parent, View view, int position, long id) - { - int menuIndex = 0; - if (position == menuIndex++) - { - final ProgressDialog pd = ProgressDialog.show(MainActivity.this, "", "努力运行中, 请稍后......"); - miband.connect(device, new ActionCallback() - { - - @Override - public void onSuccess(Object data) - { - pd.dismiss(); - Log.d(TAG, - "连接成功!!!"); - - miband.setDisconnectedListener(new NotifyListener() - { - @Override - public void onNotify(byte[] data) - { - Log.d(TAG, - "连接断开!!!"); - } - }); - } - - @Override - public void onFail(int errorCode, String msg) - { - pd.dismiss(); - Log.d(TAG, "connect fail, code:" + errorCode + ",mgs:" + msg); - } - }); - } - else if (position == menuIndex++) - { - miband.showServicesAndCharacteristics(); - } - else if (position == menuIndex++) - { - miband.readRssi(new ActionCallback() - { - - @Override - public void onSuccess(Object data) - { - Log.d(TAG, "rssi:" + (int) data); - } - - @Override - public void onFail(int errorCode, String msg) - { - Log.d(TAG, "readRssi fail"); - } - }); - } - else if (position == menuIndex++) - { - miband.getBatteryInfo(new ActionCallback() - { - - @Override - public void onSuccess(Object data) - { - BatteryInfo info = (BatteryInfo) data; - Log.d(TAG, info.toString()); - } - - @Override - public void onFail(int errorCode, String msg) - { - Log.d(TAG, "getBatteryInfo fail"); - } - }); - } - else if (position == menuIndex++) - { - UserInfo userInfo = new UserInfo(20271234, 1, 32, 160, 40, "1哈哈", 0); - Log.d(TAG, "setUserInfo:" + userInfo.toString() + ",data:" + Arrays.toString(userInfo.getBytes(miband.getDevice().getAddress()))); - miband.setUserInfo(userInfo); - } - - else if (position == menuIndex++) - { - - miband.setHeartRateScanListener(new HeartRateNotifyListener() - { - @Override - public void onNotify(int heartRate) - { - Log.d(TAG, "heart rate: "+ heartRate); - } - }); - } - else if (position == menuIndex++) - { - miband.startHeartRateScan(); - } - else if (position == menuIndex++) - { - miband.startVibration(VibrationMode.VIBRATION_WITH_LED); - } - else if (position == menuIndex++) - { - miband.startVibration(VibrationMode.VIBRATION_WITHOUT_LED); - } - else if (position == menuIndex++) - { - miband.startVibration(VibrationMode.VIBRATION_10_TIMES_WITH_LED); - } - else if (position == menuIndex++) - { - miband.stopVibration(); - } - else if (position == menuIndex++) - { - miband.setNormalNotifyListener(new NotifyListener() - { - - @Override - public void onNotify(byte[] data) - { - Log.d(TAG, "NormalNotifyListener:" + Arrays.toString(data)); - } - }); - } - else if (position == menuIndex++) - { - miband.setRealtimeStepsNotifyListener(new RealtimeStepsNotifyListener() - { - - @Override - public void onNotify(int steps) - { - Log.d(TAG, "RealtimeStepsNotifyListener:" + steps); - } - }); - } - else if (position == menuIndex++) - { - miband.enableRealtimeStepsNotify(); - } - else if (position == menuIndex++) - { - miband.disableRealtimeStepsNotify(); - } - else if (position == menuIndex++) - { - miband.setLedColor(LedColor.ORANGE); - } - else if (position == menuIndex++) - { - miband.setLedColor(LedColor.BLUE); - } - else if (position == menuIndex++) - { - miband.setLedColor(LedColor.RED); - } - else if (position == menuIndex++) - { - miband.setLedColor(LedColor.GREEN); - } - else if (position == menuIndex++) - { - miband.setSensorDataNotifyListener(new NotifyListener() - { - @Override - public void onNotify(byte[] data) - { - ByteBuffer byteBuffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); - int i = 0; - - int index = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; - int d1 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; - int d2 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; - int d3 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; - - Message m = new Message(); - m.what = Message_What_ShowLog; - m.obj = index + "," + d1 + "," + d2 + "," + d3; - - handler.sendMessage(m); - } - }); - } - else if (position == menuIndex++) - { - miband.enableSensorDataNotify(); - } - else if (position == menuIndex++) - { - miband.disableSensorDataNotify(); - } - else if (position == menuIndex++) - { - miband.pair(new ActionCallback() - { - - @Override - public void onSuccess(Object data) - { - Log.d(TAG, "pair succ"); - } +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; - @Override - public void onFail(int errorCode, String msg) - { - Log.d(TAG, "pair fail"); - } - }); - } - } - }); - - } +public class MainActivity extends Activity { + static final String[] BUTTONS = new String[]{ + "Connect", + "showServicesAndCharacteristics", + "read_rssi", + "battery_info", + "setUserInfo", + "setHeartRateNotifyListener", + "startHeartRateScan", + "miband.startVibration(VibrationMode.VIBRATION_WITH_LED);", + "miband.startVibration(VibrationMode.VIBRATION_WITHOUT_LED);", + "miband.startVibration(VibrationMode.VIBRATION_10_TIMES_WITH_LED);", + "stopVibration", + "setNormalNotifyListener", + "setRealtimeStepsNotifyListener", + "enableRealtimeStepsNotify", + "disableRealtimeStepsNotify", + "miband.setLedColor(LedColor.ORANGE);", + "miband.setLedColor(LedColor.BLUE);", + "miband.setLedColor(LedColor.RED);", + "miband.setLedColor(LedColor.GREEN);", + "setSensorDataNotifyListener", + "enableSensorDataNotify", + "disableSensorDataNotify", + "pair", + }; + private static final String TAG = "==[mibandtest]=="; + private static final int Message_What_ShowLog = 1; + private MiBand miband; + private TextView logView; + private Handler handler = new Handler(Looper.getMainLooper()) { + public void handleMessage(Message m) { + switch (m.what) { + case Message_What_ShowLog: + String text = (String) m.obj; + logView.setText(text); + break; + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + + this.logView = (TextView) findViewById(R.id.textView); + + Intent intent = this.getIntent(); + final BluetoothDevice device = intent.getParcelableExtra("device"); + + + miband = new MiBand(this); + ListView lv = (ListView) findViewById(R.id.listView); + lv.setAdapter(new ArrayAdapter(this, R.layout.item, BUTTONS)); + lv.setOnItemClickListener(new OnItemClickListener() { + public void onItemClick(AdapterView parent, View view, int position, long id) { + int menuIndex = 0; + if (position == menuIndex++) { + final ProgressDialog pd = ProgressDialog.show(MainActivity.this, "", "努力运行中, 请稍后......"); + miband.connect(device, new ActionCallback() { + + @Override + public void onSuccess(Object data) { + pd.dismiss(); + Log.d(TAG, + "连接成功!!!"); + + miband.setDisconnectedListener(new NotifyListener() { + @Override + public void onNotify(byte[] data) { + Log.d(TAG, + "连接断开!!!"); + } + }); + } + + @Override + public void onFail(int errorCode, String msg) { + pd.dismiss(); + Log.d(TAG, "connect fail, code:" + errorCode + ",mgs:" + msg); + } + }); + } else if (position == menuIndex++) { + miband.showServicesAndCharacteristics(); + } else if (position == menuIndex++) { + miband.readRssi(new ActionCallback() { + + @Override + public void onSuccess(Object data) { + Log.d(TAG, "rssi:" + (int) data); + } + + @Override + public void onFail(int errorCode, String msg) { + Log.d(TAG, "readRssi fail"); + } + }); + } else if (position == menuIndex++) { + miband.getBatteryInfo(new ActionCallback() { + + @Override + public void onSuccess(Object data) { + BatteryInfo info = (BatteryInfo) data; + Log.d(TAG, info.toString()); + } + + @Override + public void onFail(int errorCode, String msg) { + Log.d(TAG, "getBatteryInfo fail"); + } + }); + } else if (position == menuIndex++) { + UserInfo userInfo = new UserInfo(20271234, 1, 32, 160, 40, "1哈哈", 0); + Log.d(TAG, "setUserInfo:" + userInfo.toString() + ",data:" + Arrays.toString(userInfo.getBytes(miband.getDevice().getAddress()))); + miband.setUserInfo(userInfo); + } else if (position == menuIndex++) { + + miband.setHeartRateScanListener(new HeartRateNotifyListener() { + @Override + public void onNotify(int heartRate) { + Log.d(TAG, "heart rate: " + heartRate); + } + }); + } else if (position == menuIndex++) { + miband.startHeartRateScan(); + } else if (position == menuIndex++) { + miband.startVibration(VibrationMode.VIBRATION_WITH_LED); + } else if (position == menuIndex++) { + miband.startVibration(VibrationMode.VIBRATION_WITHOUT_LED); + } else if (position == menuIndex++) { + miband.startVibration(VibrationMode.VIBRATION_10_TIMES_WITH_LED); + } else if (position == menuIndex++) { + miband.stopVibration(); + } else if (position == menuIndex++) { + miband.setNormalNotifyListener(new NotifyListener() { + + @Override + public void onNotify(byte[] data) { + Log.d(TAG, "NormalNotifyListener:" + Arrays.toString(data)); + } + }); + } else if (position == menuIndex++) { + miband.setRealtimeStepsNotifyListener(new RealtimeStepsNotifyListener() { + + @Override + public void onNotify(int steps) { + Log.d(TAG, "RealtimeStepsNotifyListener:" + steps); + } + }); + } else if (position == menuIndex++) { + miband.enableRealtimeStepsNotify(); + } else if (position == menuIndex++) { + miband.disableRealtimeStepsNotify(); + } else if (position == menuIndex++) { + miband.setLedColor(LedColor.ORANGE); + } else if (position == menuIndex++) { + miband.setLedColor(LedColor.BLUE); + } else if (position == menuIndex++) { + miband.setLedColor(LedColor.RED); + } else if (position == menuIndex++) { + miband.setLedColor(LedColor.GREEN); + } else if (position == menuIndex++) { + miband.setSensorDataNotifyListener(new NotifyListener() { + @Override + public void onNotify(byte[] data) { + ByteBuffer byteBuffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); + int i = 0; + + int index = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; + int d1 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; + int d2 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; + int d3 = (data[i++] & 0xFF) | (data[i++] & 0xFF) << 8; + + Message m = new Message(); + m.what = Message_What_ShowLog; + m.obj = index + "," + d1 + "," + d2 + "," + d3; + + handler.sendMessage(m); + } + }); + } else if (position == menuIndex++) { + miband.enableSensorDataNotify(); + } else if (position == menuIndex++) { + miband.disableSensorDataNotify(); + } else if (position == menuIndex++) { + miband.pair(new ActionCallback() { + + @Override + public void onSuccess(Object data) { + Log.d(TAG, "pair succ"); + } + + @Override + public void onFail(int errorCode, String msg) { + Log.d(TAG, "pair fail"); + } + }); + } + } + }); + + } } diff --git a/app/src/main/java/com/zhaoxiaodan/mibanddemo/ScanActivity.java b/app/src/main/java/com/zhaoxiaodan/mibanddemo/ScanActivity.java index 1f885a6..bcf5363 100644 --- a/app/src/main/java/com/zhaoxiaodan/mibanddemo/ScanActivity.java +++ b/app/src/main/java/com/zhaoxiaodan/mibanddemo/ScanActivity.java @@ -1,19 +1,13 @@ package com.zhaoxiaodan.mibanddemo; import android.app.Activity; -import android.app.ProgressDialog; import android.bluetooth.BluetoothDevice; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; import android.content.Intent; -import android.database.DataSetObserver; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.util.Log; import android.view.View; -import android.widget.Adapter; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; @@ -21,108 +15,85 @@ import android.widget.ListView; import android.widget.TextView; -import com.zhaoxiaodan.miband.ActionCallback; import com.zhaoxiaodan.miband.MiBand; -import com.zhaoxiaodan.miband.listeners.NotifyListener; -import com.zhaoxiaodan.miband.listeners.RealtimeStepsNotifyListener; -import com.zhaoxiaodan.miband.model.BatteryInfo; -import com.zhaoxiaodan.miband.model.LedColor; -import com.zhaoxiaodan.miband.model.UserInfo; -import com.zhaoxiaodan.miband.model.VibrationMode; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; + import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -public class ScanActivity extends Activity -{ - private static final String TAG = "==[mibandtest]=="; - private MiBand miband; - - - HashMap devices = new HashMap(); - - - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.scan); - - miband = new MiBand(this); - - final ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, new ArrayList()); - - final ScanCallback scanCallback = new ScanCallback() - { - @Override - public void onScanResult(int callbackType, ScanResult result) - { - BluetoothDevice device = result.getDevice(); - Log.d(TAG, - "找到附近的蓝牙设备: name:" + device.getName() + ",uuid:" - + device.getUuids() + ",add:" - + device.getAddress() + ",type:" - + device.getType() + ",bondState:" - + device.getBondState() + ",rssi:" + result.getRssi()); - - String item = device.getName() + "|" + device.getAddress(); - if (!devices.containsKey(item)) - { - devices.put(item, device); - adapter.add(item); - } - - } - }; - - - ((Button) findViewById(R.id.starScanButton)).setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - Log.d(TAG, "开始扫描附近的Le蓝牙设备..."); - MiBand.startScan(scanCallback); - } - }); - - ((Button)findViewById(R.id.stopScanButton)).setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - Log.d(TAG, "停止扫描..."); - MiBand.stopScan(scanCallback); - } - }); - - - ListView lv = (ListView)findViewById(R.id.listView); - lv.setAdapter(adapter); - lv.setOnItemClickListener(new OnItemClickListener() - { - public void onItemClick(AdapterView parent, View view, int position, long id) - { - String item = ((TextView)view).getText().toString(); - if (devices.containsKey(item)) - { - - Log.d(TAG, "停止扫描..."); - MiBand.stopScan(scanCallback); - - BluetoothDevice device = devices.get(item); - Intent intent = new Intent(); - intent.putExtra("device",device); - intent.setClass(ScanActivity.this, MainActivity.class); - ScanActivity.this.startActivity(intent); - ScanActivity.this.finish(); - } - } - }); - - } +public class ScanActivity extends Activity { + private static final String TAG = "==[mibandtest]=="; + private MiBand miband; + + + HashMap devices = new HashMap(); + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.scan); + + miband = new MiBand(this); + + final ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, new ArrayList()); + + final ScanCallback scanCallback = new ScanCallback() { + @Override + public void onScanResult(int callbackType, ScanResult result) { + BluetoothDevice device = result.getDevice(); + Log.d(TAG, + "找到附近的蓝牙设备: name:" + device.getName() + ",uuid:" + + device.getUuids() + ",add:" + + device.getAddress() + ",type:" + + device.getType() + ",bondState:" + + device.getBondState() + ",rssi:" + result.getRssi()); + + String item = device.getName() + "|" + device.getAddress(); + if (!devices.containsKey(item)) { + devices.put(item, device); + adapter.add(item); + } + + } + }; + + + ((Button) findViewById(R.id.starScanButton)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Log.d(TAG, "开始扫描附近的Le蓝牙设备..."); + MiBand.startScan(scanCallback); + } + }); + + ((Button) findViewById(R.id.stopScanButton)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Log.d(TAG, "停止扫描..."); + MiBand.stopScan(scanCallback); + } + }); + + + ListView lv = (ListView) findViewById(R.id.listView); + lv.setAdapter(adapter); + lv.setOnItemClickListener(new OnItemClickListener() { + public void onItemClick(AdapterView parent, View view, int position, long id) { + String item = ((TextView) view).getText().toString(); + if (devices.containsKey(item)) { + + Log.d(TAG, "停止扫描..."); + MiBand.stopScan(scanCallback); + + BluetoothDevice device = devices.get(item); + Intent intent = new Intent(); + intent.putExtra("device", device); + intent.setClass(ScanActivity.this, MainActivity.class); + ScanActivity.this.startActivity(intent); + ScanActivity.this.finish(); + } + } + }); + + } } diff --git a/app/src/main/res/layout/item.xml b/app/src/main/res/layout/item.xml index 8f99bce..e15c1a4 100644 --- a/app/src/main/res/layout/item.xml +++ b/app/src/main/res/layout/item.xml @@ -3,6 +3,6 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" - android:textSize="16sp" > + android:textSize="16sp"> \ No newline at end of file diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index e773bb0..52f5b7a 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -1,7 +1,7 @@ + android:layout_width="match_parent" + android:layout_height="match_parent"> + android:layout_alignParentLeft="true" /> + android:layout_marginTop="31dp" /> diff --git a/app/src/main/res/layout/scan.xml b/app/src/main/res/layout/scan.xml index c267c36..fa208cf 100644 --- a/app/src/main/res/layout/scan.xml +++ b/app/src/main/res/layout/scan.xml @@ -1,7 +1,7 @@ + android:layout_width="match_parent" + android:layout_height="match_parent"> + android:layout_alignParentLeft="true" /> + android:layout_alignParentStart="true" />