Bluetooth APP Hilfe

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Bluetooth APP Hilfe

    Hallo zusammen,

    ich bin neu in der Programmierung von Android-Software sowie JAVA.
    Mein Ziel ist es ein Programm zu schreiben, welches 5 Buttons hat.

    Jeder dieser Button soll Zahlen senden.
    Butten 1 = 1
    Button 2 = 2 usw. über Bluetooth senden.
    Es gibt, sowie ich gesehen habe ein Chat beispiel über bluetooth von google, leider ist meine Sprachprogrammierung für java = 0, sodass ich hoffe, das mir hier jemand unter die Arme greifen könnte.

    Axo, ihr fragt euch bestimmt wofür das ganze.
    Die gesendeten Befehle über BT werden empfangen und über UART an einen mikrocontroller gesendet. Dieser reagiert darauf und schaltet zum Beispiel lampen ein und aus.
    Bei Youtube habe ich soetwas gefunden.


    Ich hoffe mir kann evtl. jemand ein Beispielprogramm für DKS/Eclipse schicken.
    Ich weiss, das es bestimmt den eine oder andere gibt, der sagt machs selber etc. aber ich weiss wirklich nicht wie ich anfangen soll und die Programmierung für den Uc dauert auch seine Zeit :)

    Vll hilft mir ja jemand.

    Danke
  • Hallo zusammen,

    ich habe nun folgendes App ein wenig abgeändert und es sieht wie folgt aus.
    Kann mir jemand sagen, welche Zeilen ich ändern muss, damit er sich von selbst mit einer bestimmten bluetooth verbindet ohne Connect drücken zu müssen und den BT-Dongle auszuwählen?
    Zusätzlich soll angezeigt werden, ob das APP verbunden ist oder nicht, denn zur Zeit drücke ich connect und wähle ein BT empfänger aus. Es ist vollkommen egal ob er verbunden ist oder nicht, es wechselt automatisch auf Disconnect.

    Hoffe ihr könnt mir nun helfen

    Java-Quellcode

    1. public class blu_car extends Activity {
    2. // Constants for Touch events
    3. protected static final int ACTION_DOWN = 0;
    4. protected static final int ACTION_UP = 1;
    5. protected static final int ACTION_MOVE = 2;
    6. // Intent request codes
    7. private static final int REQUEST_CONNECT_DEVICE = 1;
    8. private static final int REQUEST_ENABLE_BT = 2;
    9. // Program variables
    10. private byte AttinyOut;
    11. private boolean ledStat;
    12. private boolean connectStat = false;
    13. private Button led_button;
    14. private Button connect_button;
    15. private TextView xAccel;
    16. protected static final int MOVE_TIME = 80;
    17. private long lastWrite = 0;
    18. private AlertDialog aboutAlert;
    19. private View aboutView;
    20. // Sensor and Wakelock
    21. private SensorManager mySensorManager;
    22. private List<Sensor> sensors;
    23. private Sensor accSensor;
    24. protected PowerManager.WakeLock mWakeLock;
    25. // Bluetooth Stuff
    26. private BluetoothAdapter mBluetoothAdapter = null;
    27. private BluetoothSocket btSocket = null;
    28. private OutputStream outStream = null;
    29. private ConnectThread mConnectThread = null;
    30. private String deviceAddress = null;
    31. // Well known SPP UUID (will *probably* map to RFCOMM channel 1 (default) if not in use);
    32. // see comments in onResume().
    33. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    34. /** Called when the activity is first created. */
    35. @Override
    36. public void onCreate(Bundle savedInstanceState) {
    37. super.onCreate(savedInstanceState);
    38. setContentView(R.layout.main);
    39. // Finds buttons in .xml layout file
    40. led_button = (Button) findViewById(R.id.led_button);
    41. connect_button = (Button) findViewById(R.id.connect_button);
    42. xAccel = (TextView) findViewById(R.id.accText);
    43. // Set Sensor + Manager
    44. mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    45. sensors = mySensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    46. if(sensors.size() > 0)
    47. {
    48. accSensor = sensors.get(0);
    49. }
    50. // Create about dialog
    51. AlertDialog.Builder builder = new AlertDialog.Builder(this);
    52. // Construct about menu with weblink
    53. LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    54. aboutView = inflater.inflate(R.layout.about_text, null);
    55. // Create dialog
    56. builder.setMessage(null).setView(aboutView).setCancelable(true).setTitle(R.string.about).setIcon(R.drawable.ic_menu_info_details);
    57. aboutAlert = builder.create();
    58. // Prevent phone from sleeping
    59. final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    60. this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
    61. this.mWakeLock.acquire();
    62. // Check whether bluetooth adapter exists
    63. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    64. if (mBluetoothAdapter == null) {
    65. Toast.makeText(this, R.string.no_bt_device, Toast.LENGTH_LONG).show();
    66. finish();
    67. return;
    68. }
    69. // If BT is not on, request that it be enabled.
    70. if (!mBluetoothAdapter.isEnabled()) {
    71. Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    72. startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    73. }
    74. /**********************************************************************
    75. * Buttons for controlling BluCar
    76. */
    77. // Connect to Bluetooth Module
    78. connect_button.setOnClickListener(new View.OnClickListener() {
    79. @Override
    80. public void onClick(View v) {
    81. if (connectStat) {
    82. // Attempt to disconnect from the device
    83. disconnect();
    84. }else{
    85. // Attempt to connect to the device
    86. connect();
    87. }
    88. }
    89. });
    90. // Toggle Headlights
    91. led_button.setOnClickListener(new View.OnClickListener() {
    92. @Override
    93. public void onClick(View v) {
    94. if (ledStat) {
    95. AttinyOut = (byte) (124);
    96. led_button.setText(R.string.ledON);
    97. ledStat = false;
    98. }else{
    99. AttinyOut = (byte) (128);
    100. led_button.setText(R.string.ledOFF);
    101. ledStat = true;
    102. }
    103. write(AttinyOut);
    104. }
    105. });
    106. }
    107. /** Thread used to connect to a specified Bluetooth Device */
    108. public class ConnectThread extends Thread {
    109. private String address;
    110. ConnectThread(String MACaddress) {
    111. address = MACaddress;
    112. }
    113. public void run() {
    114. // When this returns, it will 'know' about the server,
    115. // via it's MAC address.
    116. BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    117. // We need two things before we can successfully connect
    118. // (authentication issues aside): a MAC address, which we
    119. // already have, and an RFCOMM channel.
    120. // Because RFCOMM channels (aka ports) are limited in
    121. // number, Android doesn't allow you to use them directly;
    122. // instead you request a RFCOMM mapping based on a service
    123. // ID. In our case, we will use the well-known SPP Service
    124. // ID. This ID is in UUID (GUID to you Microsofties)
    125. // format. Given the UUID, Android will handle the
    126. // mapping for you. Generally, this will return RFCOMM 1,
    127. // but not always; it depends what other BlueTooth services
    128. // are in use on your Android device.
    129. try {
    130. btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    131. } catch (IOException e) {
    132. }
    133. // Discovery may be going on, e.g., if you're running a
    134. // 'scan for devices' search from your handset's Bluetooth
    135. // settings, so we call cancelDiscovery(). It doesn't hurt
    136. // to call it, but it might hurt not to... discovery is a
    137. // heavyweight process; you don't want it in progress when
    138. // a connection attempt is made.
    139. mBluetoothAdapter.cancelDiscovery();
    140. // Blocking connect, for a simple client nothing else can
    141. // happen until a successful connection is made, so we
    142. // don't care if it blocks.
    143. try {
    144. btSocket.connect();
    145. } catch (IOException e1) {
    146. try {
    147. btSocket.close();
    148. } catch (IOException e2) {
    149. }
    150. }
    151. // Create a data stream so we can talk to server.
    152. try {
    153. outStream = btSocket.getOutputStream();
    154. } catch (IOException e2) {
    155. }
    156. }
    157. }
    158. public void connect() {
    159. // Launch the DeviceListActivity to see devices and do scan
    160. Intent serverIntent = new Intent(this, DeviceListActivity.class);
    161. startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
    162. }
    163. public void onActivityResult(int requestCode, int resultCode, Intent data) {
    164. switch (requestCode) {
    165. case REQUEST_CONNECT_DEVICE:
    166. // When DeviceListActivity returns with a device to connect
    167. if (resultCode == Activity.RESULT_OK) {
    168. // Get the device MAC address
    169. deviceAddress = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
    170. // Connect to device with specified MAC address
    171. mConnectThread = new ConnectThread(deviceAddress);
    172. mConnectThread.start();
    173. // Set button to display current status
    174. connectStat = true;
    175. connect_button.setText(R.string.connected);
    176. // Reset the BluCar
    177. AttinyOut = 0;
    178. ledStat = false;
    179. write(AttinyOut);
    180. }
    181. break;
    182. case REQUEST_ENABLE_BT:
    183. // When the request to enable Bluetooth returns
    184. if (resultCode == Activity.RESULT_OK) {
    185. // Bluetooth is now enabled
    186. } else {
    187. // User did not enable Bluetooth or an error occured
    188. Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
    189. finish();
    190. }
    191. }
    192. }
    193. public void write(byte data) {
    194. if (outStream != null) {
    195. try {
    196. outStream.write(data);
    197. } catch (IOException e) {
    198. }
    199. }
    200. }
    201. public void emptyOutStream() {
    202. if (outStream != null) {
    203. try {
    204. outStream.flush();
    205. } catch (IOException e) {
    206. }
    207. }
    208. }
    209. public void disconnect() {
    210. if (outStream != null) {
    211. try {
    212. outStream.close();
    213. connectStat = false;
    214. connect_button.setText(R.string.disconnected);
    215. } catch (IOException e) {
    216. }
    217. }
    218. }
    219. @Override
    220. public boolean onCreateOptionsMenu(Menu menu) {
    221. MenuInflater inflater = getMenuInflater();
    222. inflater.inflate(R.menu.option_menu, menu);
    223. return true;
    224. }
    225. @Override
    226. public boolean onOptionsItemSelected(MenuItem item) {
    227. switch (item.getItemId()) {
    228. case R.id.about:
    229. // Show info about the author (that's me!)
    230. aboutAlert.show();
    231. return true;
    232. case R.id.quit:
    233. // Quit the application
    234. System.exit(0);
    235. return true;
    236. }
    237. return false;
    238. }
    239. @Override
    240. public void onStart() {
    241. super.onStart();
    242. }
    243. @Override
    244. public void onResume() {
    245. super.onResume();
    246. }
    247. @Override
    248. public void onPause() {
    249. super.onPause();
    250. }
    251. @Override
    252. public void onStop() {
    253. super.onStop();
    254. }
    255. @Override
    256. public void onDestroy() {
    257. if (mWakeLock.isHeld()) {
    258. mWakeLock.release();
    259. }
    260. emptyOutStream();
    261. disconnect();
    262. super.onDestroy();
    263. }
    264. }
    Alles anzeigen