bluetooth-device-picker 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ac42be4e8eeb36fcc51b80d88360ed6d9828dee3982b4ce84828f6757da30bf8
4
+ data.tar.gz: e59c9229ef29d34af94fa25699fa6094b7cc3a21a6a192912450f4bd18772c7b
5
+ SHA512:
6
+ metadata.gz: 36aff253a5a9b94a801c1017d9d5f40f8a3ed9a7f11bec50ff20b5fadef60ff4502425595a24e31cde32f3092ea3474a83b56298065134281d5b24e4321db4d6
7
+ data.tar.gz: 6151bfa1a2beaa9f4bdee6350e435bea7ad7bce8de78c1041bb6e304d55161d7ea6f22f04f81cb79b26076d7a372f6793bce003aeb54a587bc6348e7274f33ce
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-31
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Aaron Rustad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Bluetooth::Device::Picker
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bluetooth/device/picker`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bluetooth-device-picker.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bluetooth/device/picker"
6
+
7
+ cli = Bluetooth::Device::Picker::CLI.new
8
+ cli.run(ARGV)
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "tty-command"
5
+ require "tty-prompt"
6
+ require "pastel"
7
+
8
+ module Bluetooth
9
+ module Device
10
+ module Picker
11
+ # CLI presents interactive commands for managing Bluetooth devices.
12
+ class CLI
13
+ def initialize
14
+ @cmd = TTY::Command.new(printer: :null)
15
+ @prompt = TTY::Prompt.new
16
+ @pastel = Pastel.new
17
+ end
18
+
19
+ def run(args)
20
+ ensure_blueutil_available
21
+
22
+ command = args[0]
23
+ mac_address = args[1]
24
+
25
+ # Interactive mode when no arguments provided
26
+ if command.nil?
27
+ interactive_mode
28
+ return
29
+ end
30
+
31
+ case command
32
+ when "connect"
33
+ connect(mac_address)
34
+ when "disconnect"
35
+ disconnect(mac_address)
36
+ else
37
+ puts "Usage: bluetooth-sound-picker [connect|disconnect] MAC_ADDRESS"
38
+ exit 1
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def interactive_mode
45
+ if bluetooth_devices.empty?
46
+ puts "No paired bluetooth devices found."
47
+ exit 0
48
+ end
49
+
50
+ choices = build_choices(bluetooth_devices)
51
+
52
+ # Ask user to select device
53
+ selected_display_name = @prompt.select("Toggle device:", choices.keys, filter: true)
54
+ mac_address = choices[selected_display_name]
55
+
56
+ # Toggle the connection based on current state
57
+ toggle(mac_address, bluetooth_devices[mac_address][:connected])
58
+ rescue ::TTY::Reader::InputInterrupt, Interrupt
59
+ exit 0
60
+ end
61
+
62
+ def bluetooth_devices
63
+ @bluetooth_devices ||= begin
64
+ result = @cmd.run("blueutil --paired --format json")
65
+ raw_devices = ::JSON.parse(result.out)
66
+
67
+ raw_devices.each_with_object({}) do |device, devices|
68
+ mac_address = device["address"]
69
+ next if mac_address.nil? || mac_address.empty?
70
+
71
+ devices[mac_address] = {
72
+ connected: device["connected"],
73
+ name: device["name"] || mac_address
74
+ }
75
+ end
76
+ rescue JSON::ParserError => e
77
+ puts "Error parsing bluetooth device list: #{e.message}"
78
+ exit 1
79
+ end
80
+ end
81
+
82
+ def build_choices(devices)
83
+ sorted_devices = devices.sort_by { |_mac, info| info[:name] }
84
+
85
+ sorted_devices.each_with_object({}) do |(mac, info), choices|
86
+ display_name =
87
+ if info[:connected]
88
+ "#{info[:name]} #{@pastel.green("\u2713")}"
89
+ else
90
+ info[:name]
91
+ end
92
+ choices[display_name] = mac
93
+ end
94
+ end
95
+
96
+ def toggle(mac_address, is_connected)
97
+ if is_connected
98
+ disconnect(mac_address)
99
+ else
100
+ connect(mac_address)
101
+ end
102
+ end
103
+
104
+ def connect(mac_address)
105
+ if mac_address.nil? || mac_address.empty?
106
+ puts "Error: MAC address is required"
107
+ puts "Usage: bluetooth-sound-picker connect MAC_ADDRESS"
108
+ exit 1
109
+ end
110
+
111
+ puts "Connecting to #{mac_address}..."
112
+ @cmd.run("blueutil --connect #{mac_address}")
113
+ puts "Successfully connected to #{mac_address}"
114
+ rescue TTY::Command::ExitError => e
115
+ puts "Error connecting to #{mac_address}: #{e.message}"
116
+ exit 1
117
+ end
118
+
119
+ def disconnect(mac_address)
120
+ if mac_address.nil? || mac_address.empty?
121
+ puts "Error: MAC address is required"
122
+ puts "Usage: bluetooth-sound-picker disconnect MAC_ADDRESS"
123
+ exit 1
124
+ end
125
+
126
+ puts "Disconnecting from #{mac_address}..."
127
+ @cmd.run("blueutil --disconnect #{mac_address}")
128
+ puts "Successfully disconnected from #{mac_address}"
129
+ rescue TTY::Command::ExitError => e
130
+ puts "Error disconnecting from #{mac_address}: #{e.message}"
131
+ exit 1
132
+ end
133
+
134
+ def ensure_blueutil_available
135
+ return if system("command -v blueutil >/dev/null 2>&1")
136
+
137
+ puts "Error: blueutil command not found. Please install blueutil and ensure it is available in your PATH."
138
+ exit 1
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluetooth
4
+ module Device
5
+ module Picker
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "picker/version"
4
+ require_relative "picker/cli"
5
+
6
+ module Bluetooth
7
+ module Device
8
+ module Picker
9
+ class Error < StandardError; end
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bluetooth-device-picker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Rustad
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: pastel
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: tty-command
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: tty-prompt
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.23'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.23'
54
+ description: |-
55
+ Bluetooth Device Picker provides an interactive CLI for managing Bluetooth
56
+ device connections on macOS using blueutil. Features include device listing
57
+ with connection status, filtering, and quick toggle functionality.
58
+ email:
59
+ - arustad@anassina.com
60
+ executables:
61
+ - bluetooth-device-picker
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - CHANGELOG.md
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - exe/bluetooth-device-picker
70
+ - lib/bluetooth/device/picker.rb
71
+ - lib/bluetooth/device/picker/cli.rb
72
+ - lib/bluetooth/device/picker/version.rb
73
+ homepage: https://github.com/AaronRustad/bluetooth-device-picker
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/AaronRustad/bluetooth-device-picker
78
+ source_code_uri: https://github.com/AaronRustad/bluetooth-device-picker
79
+ changelog_uri: https://github.com/AaronRustad/bluetooth-device-picker/blob/main/CHANGELOG.md
80
+ rubygems_mfa_required: 'true'
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.2.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.6.9
96
+ specification_version: 4
97
+ summary: A command-line tool to connect and disconnect Bluetooth devices on macOS
98
+ test_files: []