|
楼主 |
发表于 2015-1-18 21:08:16
|
显示全部楼层
android程式,會用就用,不會多google或是上課吧,我是google學的
public class MainActivity extends Activity{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
Handler handler = new Handler();
byte[] readBuffer;
volatile boolean stopWorker;
String readData1 = "",readData2="",wtkil;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startBT(); //開始藍牙連結
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//禁止螢幕進入休眠和鎖定
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//取消標題
setContentView(new MySurface(this));
}
@Override
public boolean onTouchEvent(MotionEvent event) { //抓取觸控點擊位置
if (event.getAction()==MotionEvent.ACTION_DOWN){ //如果點擊
movx=(int) event.getX();
movy=(int) event.getY();
}
return true;
}
void startBT() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
Toast.makeText(MainActivity.this, "開啟藍牙", Toast.LENGTH_SHORT).show();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
// 註冊一個BroadcastReceiver,等等會用來接收搜尋到裝置的消息
mBluetoothAdapter.startDiscovery(); //開始搜尋裝置
Toast.makeText(MainActivity.this, "搜索藍牙", Toast.LENGTH_SHORT).show();
}
else{
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0){
for(BluetoothDevice device : pairedDevices){
if(device.getName().equals(readData2)){ //readData2=你的藍牙名稱
mmDevice = device;
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
nobt=0;
} catch (IOException e) {
Toast.makeText(MainActivity.this, "藍牙連線失敗", Toast.LENGTH_SHORT).show();
nobt=1;
}
if (nobt==0){
Toast.makeText(MainActivity.this, "藍牙連線成功", Toast.LENGTH_SHORT).show();
beginListenForData(); //開始傾聽藍芽裝置的資料
btstart=1;
}
break;
}
}
}
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// 當收尋到裝置時
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
// 取得藍芽裝置這個物件
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 判斷那個裝置是不是你要連結的裝置,根據藍芽裝置名稱判斷
if (device.getName().equals(readData2)){
try {
// 一進來一定要停止搜尋
mBluetoothAdapter.cancelDiscovery();
// 連結到該裝置
mmSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
btstart=1;
Toast.makeText(MainActivity.this, "藍牙連線和配對成功", Toast.LENGTH_SHORT).show();
nobt=2;
} catch (IOException e) {
Toast.makeText(MainActivity.this, "找不到藍牙", Toast.LENGTH_SHORT).show();
nobt=1;
}
}
}
}
};
void findBT() {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0){
for(BluetoothDevice device : pairedDevices){
if(device.getName().equals(readData2)){ readData2=你的藍牙名稱
mmDevice = device;
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
if (nobt==2)
nobt=2;
else
nobt=0;
} catch (IOException e) {
Toast.makeText(MainActivity.this, "藍牙連線失敗", Toast.LENGTH_SHORT).show();
nobt=1;
}
if (nobt==0){
Toast.makeText(MainActivity.this, "藍牙連線成功", Toast.LENGTH_SHORT).show();
beginListenForData(); //開始傾聽藍芽裝置的資料
btstart=1;
}
break;
}
}
}
if (nobt==1){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
// 註冊一個BroadcastReceiver,等等會用來接收搜尋到裝置的消息
mBluetoothAdapter.startDiscovery(); //開始搜尋裝置
Toast.makeText(MainActivity.this, "搜索藍牙", Toast.LENGTH_SHORT).show();
}
}
void beginListenForData() {
stopWorker = false;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() { //建立一條新執行緒進入傾聽來自藍芽裝置資料輸入程序
public void run() {
while(!Thread.currentThread().isInterrupted() && !stopWorker){
try{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0){
byte[] packetBytes = new byte[1];
mmInputStream.read(packetBytes);
binput1=packetBytes[0]&0xff;
}
}
}catch (IOException ex){
stopWorker = true;
}
}
}
});
workerThread.start();
}
void sendData(int z) throws IOException {
mmOutputStream.write(z); //z是看你設多少8051才開始傳資料
}
void closeBT() throws IOException //關閉藍牙
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
}
public class MySurface extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder holder;
boolean flag;
public MySurface(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
flag = true;
this.setKeepScreenOn(true);
}
Thread t = new Thread(new Runnable() {
public void run() {
drawPicture();
}
});
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
handler.postDelayed(t, 0);
}
public void surfaceDestroyed(SurfaceHolder holder) {
handler.removeCallbacks(t);
handler.removeCallbacks(workerThread);
}
protected void drawPicture() {
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTypeface(mFace);
Canvas c = holder.lockCanvas();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(0xFF000000); //清除畫面
Rect rect = new Rect();
getDrawingRect(rect);
if (rect.width()>rect.height())
c.drawRect(new RectF(0, 0, 480, 800), mPaint);
else
c.drawRect(new RectF(0, 0, 800, 480), mPaint);
///////開始畫你要的畫面
holder.unlockCanvasAndPost(c); //顯示畫面到螢幕
handler.postDelayed(t, 0); //延遲0秒
}
}
}
8051程式,只提供藍牙方式,一樣自己google,google絕對比我說老半天還仔細
ORG 00H
JMP START
START:
MOV TMOD,#00100000B
ANL PCON,#01111111B
MOV TH1,#244 ;鮑率最低2.4K,這要跟8051外接的藍牙一樣
MOV TL1,#244
SETB TR1
MOV SCON,#01010000B
;自己放延遲0.xxx秒
START0:
JNB RI,START0
MOV A,SBUF
CLR RI
CJNE A,#1,START0 ;1是手機傳給8051一個開始接收訊號,要搭配手機程式的z
START1:
;自己放延遲0.xxx秒
MOV P1,#0FFH
MOV A,P1
MOV SBUF,A ;傳給藍牙
JNB TI,$
CLR TI
JMP START1 ;跳回START1繼續讀取資料再傳藍牙
END
|
|