❶ android-Ble藍牙開發Demo示例–掃描,連接,發送和接收數據,分包解包(附源碼)
萬物互聯的物聯網時代的已經來臨,ble藍牙開發在其中扮演著舉重若輕的角色。最近剛好閑一點,抽時間梳理下這塊的知識點。
涉及ble藍牙通訊的客戶端(開啟、掃描、連接、發送和接收數據、分包解包)和服務端(初始化廣播數據、開始廣播、配置Services、Server回調操作)整個環節以及一些常見的問題即踩過的一些坑。
比如
1、在Android不同版本或不同手機的適配問題,掃描不到藍牙設備
2、如何避免ble藍牙連接出現133錯誤?
3、單次寫的數據大小有20位元組限制,如何發送長數據
藍牙有傳統(經典)藍牙和低功耗藍牙BLE(Bluetooth Low Energy)之分,兩者的開發的API不一樣,本文主講Ble藍牙開發,傳統藍牙不展開,有需要的可以自行了解。
相對傳統藍牙,BLE低功耗藍牙,主要特點是快速搜索,快速連接,超低功耗保持連接和數據傳輸。
客戶端
服務端
Android4.3(API Level 18)開始引入BLE的核心功能並提供了相應的 API。應用程序通過這些 API 掃描藍牙設備、查詢 services、讀寫設備的 characteristics(屬性特徵)等操作。
BLE藍牙協議是GATT協議, BLE相關類不多, 全都位於android.bluetooth包和android.bluetooth.le包的幾個類:
android.bluetooth.
.BluetoothGattService 包含多個Characteristic(屬性特徵值), 含有唯一的UUID作為標識
.BluetoothGattCharacteristic 包含單個值和多個Descriptor, 含有唯一的UUID作為標識
.BluetoothGattDescriptor 對Characteristic進行描述, 含有唯一的UUID作為標識
.BluetoothGatt 客戶端相關
.BluetoothGattCallback 客戶端連接回調
.BluetoothGattServer 服務端相關
.BluetoothGattServerCallback 服務端連接回調
android.bluetooth.le.
.AdvertiseCallback 服務端的廣播回調
.AdvertiseData 服務端的廣播數據
.AdvertiseSettings 服務端的廣播設置
.BluetoothLeAdvertiser 服務端的廣播
.BluetoothLeScanner 客戶端掃描相關(Android5.0新增)
.ScanCallback 客戶端掃描回調
.ScanFilter 客戶端掃描過濾
.ScanRecord 客戶端掃描結果的廣播數據
.ScanResult 客戶端掃描結果
.ScanSettings 客戶端掃描設置
BLE設備分為兩種設備: 客戶端(也叫主機/中心設備/Central), 服務端(也叫從機/外圍設備/peripheral)
客戶端的核心類是 BluetoothGatt
服務端的核心類是 BluetoothGattServer 和 BluetoothLeAdvertiser
BLE數據的核心類是 BluetoothGattCharacteristic 和 BluetoothGattDescriptor
下面詳細講解下客戶端和服務端的開發步驟流程
安卓手機涉及藍牙許可權問題,藍牙開發需要在AndroidManifest.xml文件中添加許可權聲明:
在搜索設備之前需要詢問打開手機藍牙:
注意: BLE設備地址是動態變化(每隔一段時間都會變化),而經典藍牙設備是出廠就固定不變了!
通過掃描BLE設備,根據設備名稱區分出目標設備targetDevice,下一步實現與目標設備的連接,在連接設備之前要停止搜索藍牙;停止搜索一般需要一定的時間來完成,最好調用停止搜索函數之後加以100ms的延時,保證系統能夠完全停止搜索藍牙設備。停止搜索之後啟動連接過程;
BLE藍牙的連接方法相對簡單只需調用connectGatt方法;
參數說明
與設備建立連接之後與設備通信,整個通信過程都是在BluetoothGattCallback的非同步回調函數中完成;
BluetoothGattCallback中主要回調函數如下:
上述幾個回調函數是BLE開發中不可缺少的;
當調用targetdDevice.connectGatt(context, false, gattCallback)後系統會主動發起與BLE藍牙設備的連接,若成功連接到設備將回調onConnectionStateChange方法,其處理過程如下:
判斷newState == BluetoothGatt.STATE_CONNECTED表明此時已經成功連接到設備;
mBluetoothGatt.discoverServices();
掃描BLE設備服務是安卓系統中關於BLE藍牙開發的重要一步,一般在設備連接成功後調用,掃描到設備服務後回調onServicesDiscovered()函數,函數原型如下:
BLE藍牙開發主要有負責通信的BluetoothGattService完成的。當且稱為通信服務。通信服務通過硬體工程師提供的UUID獲取。獲取方式如下:
具體操作方式如下:
開啟監聽,即建立與設備的通信的首發數據通道,BLE開發中只有當客戶端成功開啟監聽後才能與服務端收發數據。開啟監聽的方式如下:
BLE單次寫的數據量大小是有限制的, 通常是20位元組 ,可以嘗試通過requestMTU增大,但不保證能成功。分包寫是一種解決方案,需要定義分包協議,假設每個包大小20位元組,分兩種包,數據包和非數據包。對於數據包,頭兩個位元組表示包的序號,剩下的都填充數據。對於非數據包,主要是發送一些控制信息。
監聽成功後通過向 writeCharacteristic寫入數據實現與服務端的通信。寫入方式如下:
其中:value一般為Hex格式指令,其內容由設備通信的藍牙通信協議規定;
若寫入指令成功則回調BluetoothGattCallback中的onCharacteristicWrite()方法,說明將數據已經發送給下位機;
若發送的數據符合通信協議,則服務端會向客戶端回復相應的數據。發送的數據通過回調onCharacteristicChanged()方法獲取,其處理方式如下:
通過向服務端發送指令獲取服務端的回復數據,即可完成與設備的通信過程;
當與設備完成通信之後之後一定要斷開與設備的連接。調用以下方法斷開與設備的連接:
源碼上傳在CSDN上了,有需要的可以借鑒。
=====> Android藍牙Ble通訊Demo示例源碼–掃描,連接,發送和接收數據,分包解包
BLE單次寫的數據量大小是有限制的,通常是20位元組,可以嘗試通過requestMTU增大,但不保證能成功。分包寫是一種解決方案,需要定義分包協議,假設每個包大小20位元組,分兩種包,數據包和非數據包。對於數據包,頭兩個位元組表示包的序號,剩下的都填充數據。對於非數據包,主要是發送一些控制信息。
總體流程如下:
1、定義通訊協議,如下(這里只是個舉例,可以根據項目需求擴展)
2、封裝通用發送數據介面(拆包)
該介面根據會發送數據內容按最大位元組數拆分(一般20位元組)放入隊列,拆分完後,依次從隊列里取出發送
3、封裝通用接收數據介面(組包)
該介面根據從接收的數據按協議里的定義解析數據長度判讀是否完整包,不是的話把每條消息累加起來
4、解析完整的數據包,進行業務邏輯處理
5、協議還可以引入加密解密,需要注意的選演算法參數的時候,加密後的長度最好跟原數據長度一致,這樣不會影響拆包組包
一般都是Android版本適配以及不同ROM機型(小米/紅米、華為/榮耀等)(EMUI、MIUI、ColorOS等)的許可權問題
藍牙開發中有很多問題,要靜下心分析問題,肯定可以解決的,一起加油;
❷ 用C#隨機點名小程序源代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace xxx{
class Program
{
static void Main(string[] args)
{ //讀取TXT文檔
FileStream fs1 = new FileStream(@"D:/2.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs1);
string str1 = sr.ReadToEnd();
sr.Close(); fs1.Close(); //TXT文檔中每個姓名一行,之間請已回車分隔 string[] strQ = str1.Split('\n'); //隨機提取 Random rdn = new Random();
int index = rdn.Next(0, strQ.Length);
Console.WriteLine(strQ[index]);
//return strQ[index];
}
}
}
❸ iOS藍牙通訊 CoreBluetooth的使用
蘋果的藍牙通訊使用的是CoreBluetooth.framework,實際交互是使用的CBCentralManager,搜索鏈接使用的代理是:CBCentralManagerDelegate,特性的讀取和數據交互使用的代理是CBPeripheralDelegate。本文主要目的是為了對於藍牙通訊進行梳理。
本文是以CBCentralMannager 中心模式 :以手機(app)作為中心,連接其他外設的場景(主要寫此種該模式的應用方法)
主要要使用的是CBPeripheral、CBService、CBCharacteristic
三者的關系:
一個CBPeripheral有一個或者多個CBService,而每一個CBService有一個或者多個CBCharacteristic,通過可寫的CBCharacteristic發送數據,而每一個CBCharacteristic有一個或者多個Description用於描述characteristic的信息或屬性
下面介紹一下使用藍牙設備連接的主要步奏
第一步引用頭文件、代理和管理器
初始化藍牙設備代理
搜索藍牙設備
連接設備
查找設備服務
查找服務的特性
讀取、訂閱、寫入信息
藍牙工具的源碼地址: Github
❹ android_studio手機藍牙串口通信源代碼
初涉android的藍牙操作,按照固定MAC地址連接獲取Device時,程序始終是異常終止,查了好多天代碼都沒查出原因。今天改了一下API版本,突然就成功連接了。總結之後發現果然是個坑爹之極的錯誤。
為了這種錯誤拚命查原因浪費大把時間是非常不值得的,但是問題不解決更是揪心。可惜我網路了那麼多,都沒有給出確切原因。今天特此mark,希望後來者遇到這個問題的時候能輕松解決。
下面是我的連接過程,中間崩潰原因及解決辦法。
1:用AT指令獲得藍牙串口的MAC地址,地址是簡寫的,按照常理猜測可得標准格式。
2:開一個String adress= "************" //MAC地址, String MY_UUID= "************"//UUID根據通信而定,網上都有。
3:取得本地Adapter用getDefaultAdapter(); 遠程的則用getRemoteDevice(adress); 之後便可用UUID開socket進行通信。
如果中途各種在getRemoteDevice處崩潰,大家可以查看一下當前的API版本,如果是2.1或以下版本的話,便能確定是API版本問題,只要換成2.2或者以上就都可以正常運行了~ 這么坑爹的錯誤的確很為難初學者。 唉·········· 為這種小trick浪費很多時間真是難過。
(另外有個重要地方,別忘了給manifest裡面加以下兩個藍牙操作許可權哦~)
<uses-permissionandroid:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
下面附上Android藍牙操作中用固定MAC地址傳輸信息的模板,通用搜索模式日後再補刪模板:
=null;
=null;
privateOutputStreamoutStream=null;
privateInputStreaminStream=null;
privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//這條是藍牙串口通用的UUID,不要更改
privatestaticStringaddress="00:12:02:22:06:61";//<==要連接的藍牙設備MAC地址
/*獲得通信線路過程*/
/*1:獲取本地BlueToothAdapter*/
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this,"-runthisprogram.",Toast.LENGTH_LONG).show();
finish();
return;
}
/*2:獲取遠程BlueToothDevice*/
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Can'tgetremotedevice.",Toast.LENGTH_LONG).show();
finish();
return;
}
/*3:獲得Socket*/
try{
btSocket=device.(MY_UUID);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
}
/*4:取消discovered節省資源*/
mBluetoothAdapter.cancelDiscovery();
/*5:連接*/
try{
btSocket.connect();
Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
}catch(IOExceptione){
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONRESUME:",e2);
}
}
/*此時可以通信了,放在任意函數中*/
/*try{
outStream=btSocket.getOutputStream();
inStream=btSocket.getInputStream();//可在TextView里顯示
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="1";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
*/
通用搜索模式代碼模板:
簡潔簡潔方式1 demo
作用: 用VerticalSeekBar控制一個 LED屏幕的亮暗。
直接上碼咯~
packagecom.example.seed2;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.app.Dialog;
importandroid.os.Bundle;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.UUID;
importandroid.bluetooth.BluetoothAdapter;
importandroid.bluetooth.BluetoothDevice;
importandroid.bluetooth.BluetoothSocket;
importandroid.content.DialogInterface;
importandroid.util.Log;
importandroid.view.KeyEvent;
importandroid.widget.Toast;
{
privatestaticfinalStringTAG="BluetoothTest";
=null;
=null;
privateOutputStreamoutStream=null;
privateInputStreaminStream=null;
privateVerticalSeekBarvskb=null;
privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//這條是藍牙串口通用的UUID,不要更改
privatestaticStringaddress="00:12:02:22:06:61";//<==要連接的藍牙設備MAC地址
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.vskb=(VerticalSeekBar)super.findViewById(R.id.mskb);
this.vskb.setOnSeekBarChangeListener(newOnSeekBarChangeListenerX());
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this,"-runthisprogram.",Toast.LENGTH_LONG).show();
finish();
return;
}
}
.OnSeekBarChangeListener{
publicvoidonProgressChanged(VerticalSeekBarseekBar,intprogress,booleanfromUser){
//Main.this.clue.setText(seekBar.getProgress());
/*Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}*/
}
(VerticalSeekBarseekBar){
Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
}
publicvoidonStopTrackingTouch(VerticalSeekBarseekBar){
Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
}
}
@Override
publicvoidonStart()
{
super.onStart();
}
@Override
publicvoidonResume()
{
super.onResume();
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
try{
btSocket=device.(MY_UUID);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
}
mBluetoothAdapter.cancelDiscovery();
try{
btSocket.connect();
Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
}catch(IOExceptione){
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONRESUME:",e2);
}
}
//.
/*try{
outStream=btSocket.getOutputStream();
inStream=btSocket.getInputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="read";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
intret=-1;
while(ret!=-1)
{
try{
ret=inStream.read();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
*/
}
@Override
❺ 如何實現android藍牙開發 自動配對連接,並不彈出提示框
我就開始查找怎麼關閉這個藍牙配對提示框,後面還是偉大的android源碼幫助了我。
在源碼 BluetoothDevice 類中還有兩個隱藏方法
cancelBondProcess()和cancelPairingUserInput()
這兩個方法一個是取消配對進程一個是取消用戶輸入
下面是自動配對的代碼
Mainfest,xml注冊
<receiverandroid:name=".">
<intent-filter>
<actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/>
</intent-filter>
</receiver>
自己在收到廣播時處理並將預先輸入的密碼設置進去
{
StringstrPsw="0";
@Override
publicvoidonReceive(Contextcontext,Intentintent)
{
//TODOAuto-generatedmethodstub
if(intent.getAction().equals(
"android.bluetooth.device.action.PAIRING_REQUEST"))
{
BluetoothDevicebtDevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//byte[]pinBytes=BluetoothDevice.convertPinToBytes("1234");
//device.setPin(pinBytes);
Log.i("tag11111","ddd");
try
{
ClsUtils.setPin(btDevice.getClass(),btDevice,strPsw);//手機和藍牙採集器配對
ClsUtils.createBond(btDevice.getClass(),btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
<b>/************************************藍牙配對函數***************/
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importandroid.bluetooth.BluetoothDevice;
importandroid.util.Log;
publicclassClsUtils
{
/**
*與設備配對參考源碼:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("createBond");
BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}
/**
*與設備解除配對參考源碼:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodremoveBondMethod=btClass.getMethod("removeBond");
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}
staticpublicbooleansetPin(ClassbtClass,BluetoothDevicebtDevice,
Stringstr)throwsException
{
try
{
MethodremoveBondMethod=btClass.getDeclaredMethod("setPin",
newClass[]
{byte[].class});
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice,
newObject[]
{str.getBytes()});
Log.e("returnValue",""+returnValue);
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returntrue;
}
//取消用戶輸入
(ClassbtClass,
BluetoothDevicedevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelPairingUserInput");
//cancelBondProcess()
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}
//取消配對
(ClassbtClass,
BluetoothDevicedevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelBondProcess");
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}
/**
*
*@paramclsShow
*/
(ClassclsShow)
{
try
{
//取得所有方法
Method[]hideMethod=clsShow.getMethods();
inti=0;
for(;i<hideMethod.length;i++)
{
Log.e("methodname",hideMethod[i].getName()+";andtheiis:"
+i);
}
//取得所有常量
Field[]allFields=clsShow.getFields();
for(i=0;i<allFields.length;i++)
{
Log.e("Fieldname",allFields[i].getName());
}
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}</b>
執行時直接使用:
<b>publicstaticbooleanpair(StringstrAddr,StringstrPsw)
{
booleanresult=false;
=BluetoothAdapter
.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
if(!bluetoothAdapter.isEnabled())
{
bluetoothAdapter.enable();
}
if(!BluetoothAdapter.checkBluetoothAddress(strAddr))
{//檢查藍牙地址是否有效
Log.d("mylog","devAdneffient!");
}
BluetoothDevicedevice=bluetoothAdapter.getRemoteDevice(strAddr);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED)
{
try
{
Log.d("mylog","NOTBOND_BONDED");
ClsUtils.setPin(device.getClass(),device,strPsw);//手機和藍牙採集器配對
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//配對完畢就把這個設備對象傳給全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}//
}
else
{
Log.d("mylog","HASBOND_BONDED");
try
{
ClsUtils.createBond(device.getClass(),device);
ClsUtils.setPin(device.getClass(),device,strPsw);//手機和藍牙採集器配對
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//如果綁定成功,就直接把這個設備對象傳給全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}
}
returnresult;
}</b>
❻ 盤點大學老師那些奇奇怪怪的點名方式
❼ U-GWH4740是什麼藍牙APP
藍牙串口通信手機APP。基於uni-app的低功耗藍牙工具的源代碼,在別人代碼的基礎上做了測試與修改。功能描述:藍牙搜索、藍牙連接、斷開連接、發送數據(16進制)、接收數據(16進制)並顯示。
❽ 氂牛的耳標能定位嗎
定位耳標是可以定位的。
智慧牧場方案為牧場每一頭牛羊佩戴無線定位耳標/項圈/尾標,採用GPS/北斗雙星定位器模式,牧民即可在用戶APP上了解每一頭牛羊的定位動態。
在APP上點擊手動點名按鈕可進入點名詳情頁,手動點名的原理是通過藍牙掃描實現,應用場景是牲畜趕回家的時候,在棚入口處打開藍牙對進入的牲畜進行掃描,藍牙的掃描距離是50m左右,距離過遠則掃描不到。
沒有掃描到的牛顯示為未到,點擊可直接進入到追蹤頁面進行追蹤。
❾ java點名系統源代碼
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class test {
static String[] arr;
static boolean flag=true;
static List<String> a;
static JTextField jtf;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setLayout(new GridLayout(1,2));
JButton button = new JButton("抽獎");
jtf = new JTextField();
frame.add(jtf);
frame.add(button);
a = new ArrayList<String>();
arr = new String[]{"張三","李四","王五"};
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i=0;
fond:
while(true){
String str = arr[(int)(Math.random()*arr.length)];
for(String b:a){
if(b.equals(str)){
if(i>=arr.length){
jtf.setText("沒有人了");
break fond;
}
continue fond;
}
i++;
}
a.add(str);
jtf.setText(str);
break;
}
}
});
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
❿ 用易語言製作一個隨機點名軟體,誰能把源碼發給我[email protected]
你好,這個非常簡單,用到超級列表框和編輯框就可以了。
我現在不在電腦,就用手機給你一個思路吧,首先導入姓名到超級列表裡,注意超級列表框要設置為普通報表類型
用個時鍾組件,按鈕被單擊時時鍾周期等於500也就是0.5秒,時鍾周期事件下寫代碼,首先聲明一個局部變數為次數,整數型
編輯框1.文本顏色=#黑色
編輯框1.內容=超級列表框1.取標題(0,次數)
次數=次數+1
.如果(次數=超級列表框1.取表項數()-1)
次數=0
.否則
.如果結束
按鈕停止被單擊的時候,事件下寫代碼
時鍾1.時鍾周期=0
編輯框1.文本顏色=#紅色
騷年,採納吧!