馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
package com.example.easyledout;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EasyLedOut extends AppCompatActivity {
private BluetoothSocket socket8051;
private BufferedOutputStream out8051;
private int outNum;
private Button end,Sendout;
private EditText editOutNum;
byte[] array_51;
private final UUID UID8051=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int REQUEST_ENABLE_BLUETOOTH =2;
private BluetoothAdapter bluetoothApapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy_led_out);
findId();
bluetoothApapter =BluetoothAdapter.getDefaultAdapter();
if(bluetoothApapter==null){
Toast.makeText(EasyLedOut.this,"不支援Bluetooth",Toast.LENGTH_LONG).show();
finish();
}else if(!bluetoothApapter.isEnabled()){
Intent it=new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(it, REQUEST_ENABLE_BLUETOOTH);
}
BluetoothDevice device=bluetoothApapter.getRemoteDevice("00:11:12:06:00:26");
try{
socket8051=device.createRfcommSocketToServiceRecord(UID8051);
socket8051.connect();
out8051=new BufferedOutputStream(socket8051.getOutputStream());
array_51 = BigInteger.valueOf(512+127).toByteArray();
out8051.write(array_51);
out8051.flush();
}catch (IOException e){
}
Sendout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outNum=Integer.parseInt(editOutNum.getText().toString());
array_51 = BigInteger.valueOf(512+outNum).toByteArray();
try{
out8051.write(array_51);
out8051.flush();
}catch (IOException e){
e.printStackTrace();
}
}
});
end.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(socket8051!=null)
{
try{
out8051.close();
socket8051.close();
android.os.Process.killProcess(android.os.Process.myPid());
}catch (IOException e){
e.printStackTrace();
android.os.Process.killProcess(android.os.Process.myPid());
}
}else {Toast.makeText(EasyLedOut.this,"還沒有連接成功",Toast.LENGTH_SHORT).show();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
}
private void findId() {
Sendout=(Button)findViewById(R.id.sendout);
end=(Button)findViewById(R.id.end);
editOutNum=(EditText)findViewById(R.id.editoutnum);
}
這是我的程式碼但是我依照除錯視窗去檢查感覺不出來哪裡錯可以請各位幫我看一下嗎?
|